Tests pass using "autotest" but not "rake test" using Authlogic

My tests fail when "rake test: functionals" is executed, but they pass sequentially using autotest.

The tests taking place seem to be related to Authlogic not logging the user correctly when using rake.

To facilitate user signing in tests, I have a test helper method like this:

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 for user login

My stack should / authlogic / acl 9 / factory_girl / mocha

The reason I suspect the Authlogic issue is because the failed 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> but was a redirect to <http://test.host/signin>.

 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"}

      

+2


source to share


3 answers


Autotest reads all upfront AFAIR test files (it does this with RSpec, I haven't used simple tests for a long time, so I might be wrong).

To properly test your 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 test and functional tests. When you only run functional tests, authlogic is not enabled and your tests are not running.

+2


source


I'm not sure where your problem is, but I suggest you use Cucumber for testing controllers and user interaction instead of unit tests / rspec. The reason for this is because you are running your entire application, including the authentication and authorization code you have.



+1


source


The user is obviously not logged in. It seems like Bragi Ragnarson has something to say.

Here are some other reasons to highlight the problem:

  • Understand if the test is incomplete or relies on some side effect of the Autotest. Run the test yourself:

    ruby test / functionsals / users_controllers_test.rb

  • Presumably it won't work. If it doesn't, there is some kind of global code that is called for non-functional tests by autotest. This is probably a code setup in the test / integration or test / units directories, or one of them requires.

0


source







All Articles