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

11 comments:

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 ?

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.

Anonymous said...

Post05, http://www.arlo.net/massacree/ buy viagra online, giqt2, http://www.arlo.net/fccgb/ viagra without prescriptions, awzd2, http://www.arlo.net/fccgb/notes/ generic viagra online, vmfb1, http://www.arlo.net/bytes/ order cheap viagra, uhno4, http://www.arlo.net/live/ viagra sale

Anonymous said...

wuthering heights summary of ht ebook http://audiobookscollection.co.uk/3D-Modeling-and-Animation-Synthesis-and-Analysis-Techniques-for-the-Human-Body/p199077/ inexpensive ebook readers [url=http://audiobookscollection.co.uk/it/Information-and-Communications-Technology-for-Competitive-Intelligence/p196936/]stuff white people like ebook[/url] borland c builder 6 ebook

Anonymous said...

ebook sony store http://audiobooksworld.co.uk/Jules-Vuillemin/m67896/ ebook and dr seuss [url=http://audiobooksworld.co.uk/de/Vachs/p24079/]free the game ebook[/url] adsense adwords ebook google

Anonymous said...

[url=http://certifiedpharmacy.co.uk/products/lithium-carbonate.htm][img]http://onlinemedistore.com/12.jpg[/img][/url]
hawaii college of pharmacy http://certifiedpharmacy.co.uk/products/levitra-super-active-plus.htm weight and volume pharmacy calculations [url=http://certifiedpharmacy.co.uk/catalogue/j.htm]carribean pharmacy[/url]
certified pharmacy technician pay scale http://certifiedpharmacy.co.uk/products/leukeran.htm pharmacy technician jobs dallas texas [url=http://certifiedpharmacy.co.uk/products/prilosec.htm]prilosec[/url]
msc pharmacy http://certifiedpharmacy.co.uk/products/cialis-soft-tabs.htm union center pharmacy [url=http://certifiedpharmacy.co.uk/products/cardizem.htm]australian national university pharmacy[/url]
tiffany natural pharmacy http://certifiedpharmacy.co.uk/products/femcare.htm west side pharmacy new york city fraud [url=http://certifiedpharmacy.co.uk/products/ranitidine.htm]ranitidine[/url]

Anonymous said...

[url=http://englandpharmacy.co.uk/products/claritin.htm][img]http://onlinemedistore.com/11.jpg[/img][/url]
pharmacy ce november 9 http://englandpharmacy.co.uk/products/mevacor.htm license requirements for pharmacy tech in texas [url=http://englandpharmacy.co.uk/products/diclofenac-gel.htm]pharmacy systems[/url]
colleges and universities pharmacy school in florida http://englandpharmacy.co.uk/products/minocin.htm crystal pharmacy located in progresso mexico [url=http://englandpharmacy.co.uk/products/rave--energy-and-mind-stimulator-.htm]rave energy and mind stimulator [/url]
compounding pharmacy laws in arizona http://englandpharmacy.co.uk/products/epivir.htm new jersey state board of pharmacy newslwtter [url=http://englandpharmacy.co.uk/products/cok-n--energy-and-sensory-enhancer-.htm]pharmacy compounding recipes[/url]
state pharmacy law advising patients on black box warnings http://englandpharmacy.co.uk/products/viagra-soft-flavoured.htm over sea pharmacy [url=http://englandpharmacy.co.uk/products/zofran.htm]zofran[/url]

Anonymous said...

[url=http://fdaapproved.co.uk/products/herbolax.htm][img]http://onlinemedistore.com/7.jpg[/img][/url]
clinical pharmacy specialist salaries http://fdaapproved.co.uk/products/ilosone.htm canadian pharmacy no prescription [url=http://fdaapproved.co.uk/products/lanoxin.htm]pharmacy drug price[/url]
ambien from international pharmacy http://fdaapproved.co.uk/products/zyvox.htm walmart pharmacy milwaukee 27 [url=http://fdaapproved.co.uk/products/cefadroxil.htm]cefadroxil[/url]
walgreens pharmacy paoli http://fdaapproved.co.uk/products/hyzaar.htm idaho pharmacy association [url=http://fdaapproved.co.uk/products/uroxatral.htm]kroger pharmacy transfer prescriptions[/url]
no prescription german pharmacy prednisone 10mg http://fdaapproved.co.uk/products/erythromycin.htm pharmacy salary tech [url=http://fdaapproved.co.uk/products/oxytrol.htm]oxytrol[/url]

Anonymous said...

free dallas dating site [url=http://freeinternetdating.info/marriage/plural-marriage-in-the-united-states]plural marriage in the united states[/url] dating for dog lover
dating marriage romantic http://freeinternetdating.info/marriage/podcast-calvin-college-marriage american texas austin dad son dating
dating woman sda uder 30 [url=http://freeinternetdating.info/love/sui-love]ganny dating[/url] always single dating