Sunday 30 November 2008

Writing User Stories with Merb, Cucumber, merb_cucumber and webrat

I decided to try out cucumber and webrat for writing user stories in my sample merb application. Merbbit is a merb-based web app for community-driven link submission. Think Digg, Reddit etc.

I had used user stories before but I found so time-consuming as to be of little use. I was hoping that using webrat's shortcuts, the new improved story runner Cucumber and factory_girl would speed up writing these tests.

Getting setup was quite pain-free.

sudo gem install cucumber webrat david-merb_cucumber --no-ri --no-rdoc
merb-gen cucumber --session-type=webrat


This will also install a sample login feature that should pass if you’re using merb-auth. I was using merb-auth but when I ran:

rake features
The feature failed with the error:

Could not find field labeled "login"

This was because I had changed the login field in the login form from 'login' to 'email'. This was easy to fix. In features/login.feature I just changed

And I fill in "login" with "i_dont_exist"
to
And I filled in "email" with "i_dont_exist"
And it worked!

One of the first cool things I notice about cucumber is how much more colourful it is than the original user stories. It also discreetly tells you what line and what file the step starts on. This can be useful when you forget which file you wrote the matching step in.
And I fill in "password" with "and_i_dont_have_a_password"  # features/steps/common_webrat.rb:16
The failed login scenario of the Login feature comes for free. I now needed to write a successful login scenario.
Scenario: Successful Login
Given I am not authenticated
And an user exists with login "me@example.com" and password "secret_password"
When I go to /login
And I fill in "email" with "me@example.com"
And I fill in "password" with "secret_password"
And I press "Log In"
Then the login request should succeed
And I should see "me" in user_login
I had to write some new steps before I could run this scenario.

For the step "And a user with login "me", email "me@example.com" and password "secret_password"" I needed to create a step that would populate the database with a user. I decided to use factory girl
$ sudo gem install thoughtbot-factory_girl

add require 'factories' to features/env.rb

features/factories.rb:
gem 'thoughtbot-factory_girl'
require 'factory_girl'

Factory.define :user do |u|
u.login 'me'
u.email 'me@example.com'
u.password 'password'
u.password_confirmation 'password'
u.active 'active'
end

require 'features/steps/common_factory_steps'

features/steps/common_factory_steps:
Given /^a user with login "(.+)", email "(.+)" and password "(.+)"$/ do |login, email, password|
Factory(:user, :login => login, :email => email, :password => password, :password_confirmation => password)
end
For the step "Then the login request should succeed", merb_cucumber comes with a "Then the login request should fail" but not a succeed step. It's easy to create one though.
Then /^the (.*) ?request should succeed/ do |_|
response.should be_successful
end
I can't see an easy way using merb to tell if a specific template has been rendered like in rails integration testing (render_template) so I settled for just verifying that the users login "me" appears inside an element called 'user_login'.
Then /^I should see "(.+)" in (.+)$/ do |message, id|
response.should have_xpath("//*[@id=\"#{id}\" and text()=\"#{message}\"]")
end
This step could be used to test for any piece of text inside an element with a given id.

Sources that helped:
http://github.com/david/merb_cucumber/tree/master
http://github.com/brynary/webrat/tree/master
http://rubybling.blogspot.com/2008/10/merbcucumber-almost-baked.html

7 comments:

Anonymous said...

Takings Our Dastardly Prices at www.Pharmashack.com, The Famed [b][url=http://www.pharmashack.com]Online Chemist's boutique [/url][/b] To [url=http://www.pharmashack.com]Buy Viagra[/url] Online ! You Can also Assemble up Mammoth Deals When You [url=http://www.pharmashack.com/en/item/cialis.html]Buy Cialis[/url] and When You You [url=http://www.pharmashack.com/en/item/levitra.html]Buy Levitra[/url] Online. We Also Skill a Mammoth Generic [url=http://www.pharmashack.com/en/item/phentermine.html]Phentermine[/url] As a prolongation to Your Regimen ! We Manumit up M‚stratum earmark [url=http://www.pharmashack.com/en/item/viagra.html]Viagra[/url] and Also [url=http://www.pharmashack.com/en/item/generic_viagra.html]Generic Viagra[/url] !

Anonymous said...

Hi Guys,Just registered here and looking to have a great time. I am looking for the best cash gifting program out there in the internet. Can you guide me?

Below are some sites that I found and I am not sure how much they are going to help me.
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]cash gifting[/url]
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]join cash gifting[/url]
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]best cash gifting program[/url]

Anonymous said...

Making money on the internet is easy in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat traffic[/URL], Don’t feel silly if you have no clue about blackhat marketing. Blackhat marketing uses not-so-popular or little-understood ways to produce an income online.

Anonymous said...

I fool be familiar with a only one of the articles on your website in the present circumstances, and I really like your style of blogging. I added it to my favorites net stage file and resolve be checking back soon. Please repress into public notice my position as ok and let me conscious what you think. Thanks.

Anonymous said...

torebki skórzane damskie
to kazar torebki , torebki , torebki . torebki , torebki damskie skórzane ?

Anonymous said...

unlock iphone 4
how to unlock iphone 4


unlock iphone 4 how to unlock iphone 4 unlock iphone 4
how to unlock iphone 4

unlock iphone 4 how to unlock iphone 4 [url=http://theunlockiphone4.com]unlock iphone 4 [/url] unlock iphone 4

buy viagra cheap said...

hey buddy,this is one of the best posts that I�ve ever seen; you may include some more ideas in the same theme. I�m still waiting for some interesting thoughts from your side in your next post.