使用“自动测试”测试通过但不是“耙子测试”使用Authlogic

发布于 2024-08-03 10:28:02 字数 1874 浏览 4 评论 0原文

我的测试在执行“rake test:功能”时失败,但使用自动测试一致通过。

所讨论的失败测试似乎与 Authlogic 在使用 rake 时未正确登录用户有关。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傻比既视感 2024-08-10 10:28:02

Autotest 预先读取所有测试文件 AFAIR(它使用 RSpec 来执行此操作,我已经很长时间没有使用普通测试了,所以我可能是错的)。

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

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

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.

无尽的现实 2024-08-10 10:28:02

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

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.

世界和平 2024-08-10 10:28:02

显然,用户没有登录。看来 Bragi Ragnarson 可能发现了什么。

以下是隔离问题的其他一些内容:

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

    ruby test/functions/users_controllers_test.rb

  • 大概这行不通。如果没有,则自动测试会调用一些全局代码进行非功能测试。它可能是 test/integration 或 test/units 目录中的代码设置,或者它们的要求之一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文