2

我的测试在执行“rake test:functionals”时失败,但使用自动测试始终通过。

有问题的失败测试似乎与使用 rake 时 Authlogic 未正确登录用户有关。

为了便于在测试中登录用户,我有一个测试助手方法,如下所示:

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

上述方法用于登录用户

我的堆栈是 shoulda/authlogic/acl9/factory_girl/mocha

我怀疑 Authlogic 是问题的原因是失败的测试如下所示:

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

3 回答 3

2

Autotest 预先读取所有测试文件 AFAIR(它使用 RSpec 这样做,我已经很长时间没有使用普通测试了,所以我可能错了)。

要正确测试控制器,您需要在 setUp 方法中调用 activate_authlogic。对于集成测试,这可能是自动(全局)完成的。

由于自动测试会读取所有测试,因此它会运行此全局设置并且功能测试通过。当您仅运行功能测试时,authlogic 未启用并且您的测试失败。

于 2009-09-11T10:09:40.410 回答
1

我不确定您的问题出在哪里,但我建议您使用 Cucumber 来测试控制器和用户交互,而不是单元测试/rspec。原因是您使用了整个应用程序,包括您拥有的身份验证和授权代码。

于 2009-09-11T09:54:59.367 回答
0

显然用户没有登录。似乎 Bragi Ragnarson 可能正在做某事。

这里有一些其他的东西来隔离问题:

  • 了解测试是否不完整或依赖于自动测试的某些副作用。自行运行测试:

    ruby 测试/功能/users_controllers_test.rb

  • 估计这行不通。如果没有,则自动测试会为非功能测试调用一些全局代码。它可能是 test/integration 或 test/units 目录中的代码设置,或者那里的其中一个要求。

于 2009-09-12T12:08:21.127 回答