|
Posted by Balaji D Loganathan on September 22nd, 2009
Recently I tried to write the integration tests for a Rails app which uses Authlogic for authentication. 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')
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
Note:
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. Tags: Agile, Distributed Agile, Lean, Metrics, Ruby, Ruby on Rails, TDD, Testing, Unit testingUnique Views: 1415 Total views: 1881 Follow responses at RSS 2.0. Leave a response | Trackback. 3 Responses to “Integration Testing with Webrat, RSpec (and Authlogic)”Leave a Reply |
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