代码之家  ›  专栏  ›  技术社区  ›  Tomalak

测试通过使用“自动测试”,而不是使用Authlogic的“rake测试”

  •  2
  • Tomalak  · 技术社区  · 15 年前

    做“耙”的时候我的测试失败了测试:函数“但他们总是通过自动测试。

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

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

    上述方法用于登录用户

    我之所以怀疑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 回复  |  直到 15 年前
        1
  •  2
  •   Bragi Ragnarson    15 年前

    Autotest预先读取所有的测试文件(它是用RSpec实现的,我已经很久没有使用普通测试了,所以我可能错了)。

    要正确测试控制器,需要在安装方法中调用activate\u authlogic。对于集成测试,这可能是自动(全局)完成的。

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

        2
  •  1
  •   Ariejan    15 年前

        3
  •  0
  •   ndp    15 年前

    显然用户没有登录。布拉吉·拉格纳森似乎有什么发现。

    • ruby测试/函数/用户\u控制器_测试.rb

    • 想必这行不通。如果没有,则会有一些全局代码被autotest调用用于非功能测试。它可能是test/integration或test/units目录中的代码设置,或者其中一个需要。