views:

509

answers:

3

My tests fail when doing "rake test:functionals" but they pass consistently using autotest.

The failing tests in question seems to be related to Authlogic not logging in the user properly when using rake.

For facilitating signing in a user in tests, I have a test helper method as follows:

class ActionController::TestCase
  def signin(user, role = nil)
    activate_authlogic
    UserSession.create(user)
    user.has_role!(role) if role
  end
end

The above method is used to signin a user

My stack is shoulda/authlogic/acl9/factory_girl/mocha

The reason why I suspect Authlogic being the issue is the failing tests look like this:

     54) Failure:
test: A logged in user PUT :update with valid data should redirect to user profile. (UsersControllerTest)
    [/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:202:in `__bind_1251895098_871629'
     /var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
     /var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: A logged in user PUT :update with valid data should redirect to user profile. ']:
Expected response to be a redirect to <http://test.host/users/92&gt; but was a redirect to <http://test.host/signin&gt;.

 55) Failure:
test: A logged in user PUT :update with valid data should set the flash to /updated successfully/i. (UsersControllerTest)
    [/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/assertions.rb:55:in `assert_accepts'
     /var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:41:in `__bind_1251895098_935749'
     /var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
     /var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: A logged in user PUT :update with valid data should set the flash to /updated successfully/i. ']:
Expected the flash to be set to /updated successfully/i, but was {:error=>"You must be signed in to access this page"}
+1  A: 

I'm not sure about where your problem lies, but I suggest you use Cucumber for testing controllers and user interaction instead of unit tests/rspec. The reason for that is that you exercise your entire app, including the authentication and authorization code you have.

Ariejan
+2  A: 

Autotest reads all test files upfront AFAIR (it does so with RSpec, I haven't been using plain tests for a long time now so I may be wrong).

To properly test controllers you need to call activate_authlogic in your setUp method. This is probably done automatically (globally) for integration tests.

Since autotest reads all tests it runs this global setUp and functional tests pass. When you run only functional tests authlogic is not enabled and your tests fail.

Bragi Ragnarson
A: 

Clearly the user is not getting logged in. Seems like Bragi Ragnarson might be on to something.

Here are some other things to isolate the problem:

  • Understand if the test is incomplete or relying on some side-effect of autotest. Run the test by itself:

    ruby test/functionals/users_controllers_test.rb

  • Presumably that won't work. If it doesn't, there's some global code that is getting invoked for non functional tests by autotest. It's probably code setting in the test/integration or test/units directories, or one of their requires there.

ndp