Integration Testing with Webrat, RSpec (and Authlogic)

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.

[source language=”Ruby”]
require File.expand_path(File.dirname(__FILE__) + ‘/../../spec_helper’)
require “authlogic/test_case”

describe “Testing admin access page. ” do
include Webrat::HaveTagMatcher
context “When logged in as admin” do

before(:all) do
activate_authlogic
@user = User.create!(:name => “foo bar”, :email => “foo@bar.com”, :password => “foobar”, :password_confirmation => “foobar”)
visit admin_posts_url
fill_in “user_session_email”,:with => “foo@bar.com”
fill_in “user_session_password”,:with => “foobar”
click_button “Enter”
end

it ” – should show welcome page” do
visit posts_url
assert_contain “Hello Foo Bar”
assert_have_tag “h3”, :content => “some more message”
end
…..
…..
end
end
[/source]

Note:

  • 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.

Related Posts

Comments (3)

Hi,
I think the above approach is sound. One thing that I found odd was that you are using RSpec but not any matchers. Matchers are one of the best things about RSpec IMO- but to each his own :). I also generally keep one expectation (assertion) per spec. However, since this is an integration spec and testing the same facet of behaviour it is probably fine in this example.

One more thing.. I would not say “Testing admin access page” as the description. What are you really describing here? It seems that you are describing access control or permissions of some such. I would say that instead.

I’m not super familiar with AuthLogic so I can’t comment on the those parts but other than that it looks good. Thanks for sharing!

-Ben

Hi Ben,
Thanks for your comment.

Yes, I use RSpec matchers in an another expectation like
assert_have_tag(“div”) { |n|
n.should have_tag(“h2”, :content => “Menus”)
n.should have_tag(“a”, :content => “Add User”)
}
I will change the “describe” description more meaningful as mentioned by you.
Thanks again.

Been round and round w/ this issue this week. I was unable to get UserSession.create to work in an _integration_ test. Works fine in a _functional_ test. I found that in integration tests I had to actually drive the login form. Not going to work well for OpenID 🙁

Leave a comment