Shoulda + Factory_Girl + Webrat for Rails Integration Tests
Is anyone else using shoulda, factory_girl and webrat for Rails integration tests?
I did a quick search and found Daniel Wellman using it in integration tests minus shoulda and factory_girl. I really like adding shoulda to get the contexts and factory_girl is a great replacement for fixtures. When combined with webrat it really makes for some easy to read integration tests. By the way, I'm already using shoulda and factory_girl for my unit and functional tests.
Here is an example I whipped up last night. It's nothing fancy. I'll probably make some modifications in the future, but so far I'm happy with it. I'm loading webrat in my test_helper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
require '../test_helper' class AdminLoginTest < ActionController::IntegrationTest context 'An employee' do setup do @employee = Factory(:employee) end should 'be able to login with a valid username and password' do visit '/admin' assert https? assert_equal '/admin/sessions/new', path fills_in 'Email', :with => @employee.user.email fills_in 'Password', :with => @employee.user.password clicks_button 'Log In' assert_equal @employee.user.id, session[:user_id] end should 'be denied access with an invalid username or password' do visit '/admin' assert https? assert_equal '/admin/sessions/new', path fills_in 'Email', :with => @employee.user.email fills_in 'Password', :with => 'wrongpassord' clicks_button 'Log In' assert_nil session[:user_id] end end end |
This is testing a simple login to the admin section of a project I'm working on. We're using restful_authentication. Each line of each test is essentially an assertion. If something doesn't respond with a success or a form field is missing, the test fails.

Morgan Roderick said
Nov 26, 2008 @ 12:22 PM
We're using shoulda / factory_girl / webrat for testing on one project. It is quite a thrill to work with. Shoulda macros goes a long way to making integration tests more readable, especially when coupled with hpricot. Yes, once they break, it can sometimes be a bit of a pain figuring out where an error originated. Perhaps I should do a blog entry with example shoulda macros, to get some feedback on the sanity of them? Webrat does have a very annoying feature where it opens browser windows for broken tests, when running through autotest on OS X. I haven't been able to eliminate this issue, and it even happens on our CruiseControl.rb server, which is running OS X Server.Bryan Helmkamp said
Nov 28, 2008 @ 02:00 PM
Glad you've been enjoying Webrat. Please let me know if there's anything that gives you trouble. Morgan -- In the latest Webrat code, there's a setting to disable this. Try: Webrat.configure do |config| config.open_error_files = false end Latest code is at: http://github.com/brynary/webratMorgan Roderick said
Nov 29, 2008 @ 02:50 AM
Bryan, that's cool. I'll be sure to upgrade our project with a later version of Webrat, and will check out the configuration options :-)RSS feed for comments on this post
Leave a Comment