Recently I tried to write the integration tests for a Rails app which uses Authlogic for authentication.
We decided to use RSpec for unit and functional testing, and Webrat + RSpec combo for integration testing. That means Webrat with Rspec without Cucumber. The fun started when we want to integrate webrat + rspec with authlogic.
When I was looking for best way to hook webrat with rspec without cucumber, a friend of mine Olga Tan pointed me to this cool link http://reborg.tumblr.com/post/99813407/webrat-with-rspec-no-cucumber. It helped us to have our views testing integrated with Rspec.
Now I can use rake spec:integration to run the integration tests located at spec/integration folder.
I was trying to find various ways to have the Authlogic integrated with Webrat so that I can have the authenticated user session available to various test cases.
require File.expand_path(File.dirname(__FILE__) + ‘/../../spec_helper’)
describe “Testing admin access page. ” do
context “When logged in as admin” do
@user = User.create!(:name => “foo bar”, :email => “firstname.lastname@example.org”, :password => “foobar”, :password_confirmation => “foobar”)
fill_in “user_session_email”,:with => “email@example.com”
fill_in “user_session_password”,:with => “foobar”
it ” – should show welcome page” do
assert_contain “Hello Foo Bar”
assert_have_tag “h3”, :content => “some more message”
- You may have to set attr_accessible for some of your attributed in User model
- It better to use before(:all) do .. end, than before(:each) do ..end, this will avoid sequential call to setup
- If you want to use Webrat matcher tags like have_tag, have_xpath_tag, then do include Webrat::HaveTagMatcher
- Use LazyDeveloper plugin to have cool shortcuts for rake commands, thats like rake spec:model:user
Thats the way we found it for now. I would be glad if some RoR TDD gurus can validate the above approach and make some suggestions (and critiques).
If you know any other smart way, please post it as comment.