Sponsorship

40% off your First Order with GoDaddy.com!

Mar 30, 2009

Behavior Driven Development on Rails: Part 2



The cool thing about Cucumber is how it allows you to reuse your step definitions. It allows you to effectively create a DSL whereby you can generate new tests with little or no additional coding. As you’re writing step definitions keep this in mind, and look for patterns that you can dry up.

Below is an example of a step definition that has grown over time. These steps support a user administration feature using restful_authentication and aasm_state.

Given /^a user that is pending$/ do
pending
end

When /^I activate the user$/ do
pending
end

Then /^the user should be able to log in$/ do
pending
end

Given /^a user that is active$/ do
pending
end

When /^I suspend the user$/ do
pending
end

Then /^the user should be in a suspended state$/ do
pending
end

Then /^the user should not be able to log in$/ do
pending
end

Given /^a user that is suspended$/ do
pending
end

When /^I unsuspend the user$/ do
pending
end

Then /^the user should be in a pending state$/ do
pending
end

When /^I delete the user$/ do
pending
end

Immediately you should recognize that there are some patterns here: given something about the user’s state, and when the admin does something to the state, and whether or not you can log in.

Let’s see if we can’t boil these down these 20 some steps into just 4-5? First lets aggregate the common Given’s, using the power of regular expression we can do something like this:

Given /^a user that is (.+)$/ do
end

Hmm, interesting — this might work, so lets try finishing it up:

Given /^a user that is (.+)$/ do |state|
@user = User.create!(:login => 'testuser', :email => 'test@test.gov', :password => 'testme', :password_confirmation => 'testme', :state =>state)
end

Odd, the test is failing still. Well if you understand how restful_authentication works with aasm you’ll soon discover that any user created on the back-end starts out in a passive state. This actually is going to take some thought, so lets come back to it.

How about all the “When I” steps? Those are easily grouped together. Since restful_auth uses nice verbs for state transitions we can leverage this in our step definitions:

When /^I (.+) the user$/ do |state|
@user.send("#{state}!")
end

Along the same line of thinking we can apply this to our “Then the user” steps:

Then /^the user should not be able to log in$/ do
visit login_path
fill_in "login", :with => @user.login
fill_in "password", :with => @password
click_button "Log In"
response.should contain("Couldn't log you in")
end

Then /^the user should be able to log in$/ do
visit login_path
fill_in "login", :with => @user.login
fill_in "password", :with => @password
click_button "Log In"
response.should contain("Logged in successfully")
end

Then /^the user should be in a (.+) state$/ do |state|
@user.send("#{state}?")
end

There’s a lot going on in the log-in steps, we take the same exact steps to log in a user, the only change is the outcome we expect. Ok so lets refactor that out into a method, and we get:

Then /^the user should not be able to log in$/ do
login_using_form("Couldn't log you in")
end

Then /^the user should be able to log in$/ do
login_using_form("Logged in successfully")
end

def login_using_form(expectation)
visit login_path
fill_in "login", :with => @user.login
fill_in "password", :with => @password
click_button "Log In"
response.should contain(expectation)
end

Ok, better, so what about the given a user? Anything we create on the backend will start out in a state of passive (short of changing how restful_auth works). Also not all the verbs we would use in a step definition equate to a valid transition in aasm (you cannot transition to passive or pending directly). Well it takes a little more thought, but in the end you might end up with a method like the one below:

Given /^a user that is (.+)$/ do |state|
state_hash = { :active => "activate!", :suspended => "suspend!" }
@password = 'testme'
@user = User.create!(:login => 'testuser', :email => 'test@test.gov', :password => @password, :password_confirmation => @password)
##
# Restful_Authentication doesn't provide direct state transistions for passive or pending,
# so we do a little tweaking to our user object to get it into the desired state.
#
@user.register! unless state == 'passive' # new accounts created through backend start out as passive
unless state == 'pending' # accounts when registered become pending
@user.send(state_hash[state.to_sym]) # pending can transition easily to any state
end
end

Putting it all together we have a step definition file now of only 5 definitions and one helper method. These steps can support a wide combination of user starting states, user state transitions and expectations around logging in and user state post transition. Let the business folks go hog wild!

Given /^a user that is (.+)$/ do |state|
state_hash = { :active => "activate!", :suspended => "suspend!" }
@password = 'testme'
@user = User.create!(:login => 'testuser', :email => 'test@test.gov', :password => @password, :password_confirmation => @password)
##
# Restful_Authentication doesn't provide direct state transistions for passive or pending,
# so we do a little tweaking to our user object to get it into the desired state.
#
@user.register! unless state == 'passive' # new accounts created through backend start out as passive
unless state == 'pending' # accounts when registered become pending
@user.send(state_hash[state.to_sym]) # pending can transition easily to any state
end
end

When /^I (.+) the user$/ do |state|
@user.send("#{state}!")
end

Then /^the user should not be able to log in$/ do
login_using_form("Couldn't log you in")
end

Then /^the user should be able to log in$/ do
login_using_form("Logged in successfully")
end

Then /^the user should be in a (.+) state$/ do |state|
@user.send("#{state}?")
end

# step helpers --------------------------------------#

# this method simulates a login
def login_using_form(expectation)
visit login_path
fill_in "login", :with => @user.login
fill_in "password", :with => @password
click_button "Log In"
response.should contain(expectation)
end

A real-life feature that these steps support:

Feature: User Administration

So that I can control access to the application
As an admin
I want to manage users

Scenario: Activate a pending user
Given a user that is pending
When I activate the user
Then the user should be able to log in

Scenario: Suspend a user that is active
Given a user that is active
When I suspend the user
Then the user should not be able to log in

Scenario: Unsuspend a user that is suspended
Given a user that is suspended
When I unsuspend the user
Then the user should be in a pending state
And the user should not be able to log in

Scenario: Purge a user that is active
Given a user that is active
When I delete the user
Then the user should not be able to log in

Scenario: Purge a user that is pending
Given a user that is pending
When I delete the user
Then the user should not be able to log in

Scenario: Purge a user that is suspended
Given a user that is suspended
When I delete the user
Then the user should not be able to log in

Mar 26, 2009

A Beautiful Birthday Message!

Jin-Kang Cheng 25th Birthday

Behavior Driven Development on Rails: Part 1



Before we can begin doing BDD (behavior driven development) we need to install some tools to help.

sudo gem install cucumber rspec-rails webrat

Next I go to my Rails application and run some scripts to get the basic set-up inside my rails directory structure:

script/generate rspec
script/generate cucumber

Now in the newly created features folder I will create a file called user_administration.feature. This file will contain all my scenarios for this feature. After spending some time thinking about how to express my features I come up with two basic ones to start:

Feature: User Administration

So that I can control access to the application
As an admin
I want to manage users

Scenario: Create a new user account
Given no user "joetest"
When I create a new user "joetest" with password "skymonkey"
Then the user "joetest" can log in with password "skymonkey"

Scenario: Suspend an existing user account
Given a user that is active
When I suspend the user
Then the user can not log in

Now I run the following command to have cucumber generate snippet methods that I can then copy into a step definition.

script/cucumber features/user_administration.feature

I create a new file named user_steps.rb in the features/step_definitions folder. Now that we have our step definition snippets the next job is to take them from “pending” into something meaningful. We’ll start with the first scenario. After giving it some thought it might end up looking like this:

Given /^no user (.+)$/ do |login|
lambda{ User.find(login) }.should raise_error(ActiveRecord::RecordNotFound)
end

This first part sets us up. It allows us to express the given state, in this case not finding a user named “joetest”.

When /^I create a new user "(.+)" with password "(.+)"$/ do |login, password|
visit new_user_path
fill_in "user_login", :with => login
fill_in "user_email", :with => "#{login}@test.org"
fill_in "user_password", :with => password
fill_in "user_password_confirmation", :with => password
click_button "Sign Up"
end

Next we describe the action we’ll be taking. This code simulate the submission of a form, using Webrat. We take this approach because it makes us exercise the whole stack views, controller and models. It’s just short of me going to the site with a browser.

Then /^the user "(.+)" can log in with password "(.+)"$/ do |login, password|
visit login_path
fill_in "login", :with => login
fill_in "password", :with => password
click_button "Log In"
response.should contain("Logged in Successfully")
end

Lastly we test our outcome, that the user the admin created is able to log in. So the end result is test that describes the behavior of an admin creating a new account for a user, and they are able to successfully log in.

Along the way you may find tests go red (fail), this is normal. You want to write code (just enough) to make them go green, then continue to write more test code. Keep repeating this until all your tests run green.

Also the nice thing about step definitions is that they can be reused in other tests. We’ll likely have other scenarios where we create a new user or try to log in as one. Our code stays dry and we (the developer) stay confident. (Now it’s sounding like a anti-perspirant commercial, so I’ll stop now.)