gpt4 book ai didi

ruby-on-rails-4 - 使用 rails 4 和 rspec 3 被 verify_partial_doubles 绊倒

转载 作者:行者123 更新时间:2023-12-04 20:41:25 24 4
gpt4 key购买 nike

我正在使用 authlogic 进行用户身份验证,在我的 ApplicationController 中,我定义了“current_user”、“current_user_session”等并将其设置为 helper_methods。

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

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

当然,建议将 verify_partial_doubles 设置为 true,但这样做会中断。我直接从文档中提取了这个:

https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/view-specs/view-spec#passing-view-spec-that-stubs-a-helper-method

如果该方法出现在 ApplicationHelper 中,它将起作用。但如果它在 ApplicationController 中并定义为 helper_method 就没有这样的运气了:
helper_method :current_user, ...

def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end

我想要 verify_partial_doubles 提供的保护,我该如何解决这个问题?

最佳答案

这是一个已知问题,使其工作的唯一方法是将方法提取到模块中并将其包含在您的 View 助手和 Controller 中。

更多信息请访问:https://github.com/rspec/rspec-rails/issues/1076

关于ruby-on-rails-4 - 使用 rails 4 和 rspec 3 被 verify_partial_doubles 绊倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25788842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com