测试使用“自动测试”但不使用Authlogic进行“rake测试”

时间:2009-09-02 12:41:07

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

我的测试在执行“rake test:functionals”时失败,但是它们使用自动测试一致地传递。

有问题的失败测试似乎与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"}

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

显然,用户没有登录。看起来像Bragi Ragnarson可能会有所作为。

以下是其他一些可以解决问题的方法:

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

    ruby​​ test / functionals / users_controllers_test.rb

  • 据推测,这是行不通的。如果没有,则会有一些全局代码被自动测试用于非功能测试。它可能是测试/集成或测试/单元目录中的代码设置,或者是其中的一个需求。

相关问题