Ruby on rails 4 通过与rails 4和rspec 3的verify_partial_Double验证被绊倒

Ruby on rails 4 通过与rails 4和rspec 3的verify_partial_Double验证被绊倒,ruby-on-rails-4,mocking,authlogic,rspec3,Ruby On Rails 4,Mocking,Authlogic,Rspec3,我正在使用authlogic进行用户身份验证,在我的ApplicationController中,我将“当前用户”、“当前用户会话”等定义并设置为helper方法 我的主索引有一个非常简单的视图规范: RSpec.describe "main/index.html.erb", :type => :view do context "when not logged in" do before do allow(view).to receive(:current_use

我正在使用authlogic进行用户身份验证,在我的ApplicationController中,我将“当前用户”、“当前用户会话”等定义并设置为helper方法

我的主索引有一个非常简单的视图规范:

RSpec.describe "main/index.html.erb", :type => :view do
  context "when not logged in" do

    before do
      allow(view).to receive(:current_user).and_return(nil)
    end

    it "has an h1" do
      render
      expect(rendered).to include('h1')
    end

  end
end
问题在于,如果在我的配置中出现“mocks.verify_partial_doubles=true”,则会导致一个巨大的错误,因为它会转储整个对象,然后在底部显示:

  1) main/index.html.erb when not logged in has an h1
     Failure/Error: allow(view).to receive(:current_user).and_return(nil)
       #<#<Class:0x00000104c249d0>:.........
       @rendered_views={}>> does not implement: current_user

我想要验证\u partial \u doubles提供的保护,我如何解决这个问题?

这是一个已知的问题,使其工作的唯一方法是将方法提取到模块中,并将其包含在视图帮助程序和控制器中


有关详细信息,请访问:

您可以禁用视图的双重验证,如下所示:

RSpec.configure do |config|
  config.before(:each, type: :view) do
    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = false
    end
  end

  config.after(:each, type: :view) do
    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = true
    end
  end
end
这样,您就可以使用以下工具保留存根视图方法:

allow(view).to receive(:current_user).and_return(nil)

更多信息请访问:

谢谢您的回答。太棒了。这很有帮助。谢谢,谢谢你。我只是觉得这太难看了,坦率地说,这是rspec的一个重大失败。整个“当前用户作为app controller中的助手方法”设置非常常见,不需要花费很多精力就能实现。
allow(view).to receive(:current_user).and_return(nil)