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

用designe进行钢轨功能试验

  •  19
  • allesklar  · 技术社区  · 14 年前

    我的最后3个失败的测试都与designe gem及其用户有关!方法在我的控制器顶部的before\u过滤器中。

    你将通过帮助我解决这个问题来赚取巨大的业力,因为它将使我从现在开始使用TDD方法。

    下面是困扰我的错误:

    1) Error:
    test_should_get_accepted(ModerationControllerTest):
    NoMethodError: undefined method `authenticate!' for nil:NilClass
        /test/functional/moderation_controller_test.rb:10:in `test_should_get_accepted'
    

    http://github.com/plataformatec/devise 但我只是不知道该如何应用它。

    你能给这个测试noob一些关于如何使用这些助手的详细说明吗?

    5 回复  |  直到 14 年前
        1
  •  51
  •   vdaubry    13 年前

    我花了一段时间,但我找到了路。这是给那些被困在同一点上的人的:

    在decidition\u controller\u test.rb的顶部,类声明的下面,添加以下行:

    include Devise::TestHelpers
    

    在我的用户fixture中有2条记录,我在每个测试中都添加了这一行,其中必须授权用户执行操作。

    sign_in User.first
    

    当然,一旦你知道怎么做就很简单了。

        2
  •  19
  •   Mirv - Matt    8 年前

    如果您希望designe测试助手可用于您的所有测试,您必须将allesklar提到的include附在 test_helper.rb 在这样的类声明中:

    class ActionController::TestCase
      include Devise::TestHelpers
    end
    

    更新:01.25.2017

    ... rails 5发布了一个弃用警告&请使用。。。

    Devise::Test::ControllerHelpers

        3
  •  7
  •   hiwaylon    14 年前

    关于用户fixture,我必须定义一个fixture,但将其保留为空以使其工作:

    # in users.yml    
    joe: {}
    

    使用该装置时 sign_in 助手,您可以在测试中直接访问哈希对象:

    # a test method in some_controller_test.rb
    sign_in users(:joe)
    

    看到了吗 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures 有关Rails固定装置的详细信息。

        4
  •  1
  •   sameers    11 年前

    逐字引用 https://github.com/plataformatec/devise :

    如果您使用的是RSpec,那么可以将以下内容放入名为 spec/support/devise.rb

    RSpec.configure do |config|

    config.include Devise::TestHelpers, :type => :controller

    end

    sign_in sign_out 在你的RSpec测试中。

        5
  •  0
  •   dlp21103    11 年前

    除了test\u helpers.rb中的代码之外,我在controller\u测试的顶部添加了这个代码,它对我很有用:

    require 'test_helper'