ruby-on-rails - 使用 "autotest"测试通过,但使用 Authlogic 测试未通过 "rake test"

标签 ruby-on-rails testing rake authlogic shoulda

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

所讨论的失败测试似乎与 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"}

最佳答案

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

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

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

关于ruby-on-rails - 使用 "autotest"测试通过,但使用 Authlogic 测试未通过 "rake test",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1367464/

相关文章:

ruby-on-rails - 如何在 Rails 的部分模板中有一个可选的局部变量?

mysql - ActiveRecord 不更新属性

testing - 在 wxPython 应用程序的自动化测试中处理模态对话框

ruby-on-rails-3 - rake 迁移中止 : error with uninitialized constant and migration naming

ruby-on-rails - 如何破解 boot.rb 以停止烦人的警告? Gem::SourceIndex#search 对字符串模式的支持已弃用

ruby-on-rails - 在 Rails 测试环境中调用方法后对象为 nil

ruby-on-rails - Textmate 到处检查拼写

PHPunit:我可以模拟我测试的类吗?

testing - 无法使用测试用户通过 Open Graph 发帖

mysql - rake db :migrate, 源和目标