【问题标题】:Tests pass using "autotest" but not "rake test" using Authlogic测试通过使用“自动测试”但不是使用 Authlogic 的“rake 测试”
【发布时间】:2009-09-02 12:41:07
【问题描述】:

我的测试在执行“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"}

【问题讨论】:

    标签: ruby-on-rails testing rake authlogic shoulda


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

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

          ruby 测试/功能/users_controllers_test.rb

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-06
          • 2011-03-05
          • 2014-01-04
          • 2017-09-18
          • 1970-01-01
          • 2016-09-05
          • 1970-01-01
          • 2020-09-30
          相关资源
          最近更新 更多