我使用authlogic进行用户身份验证,并在我的ApplicationController中定义了“current_user”,“current_user_session”等,并将其设置为helper_methods。通过verify_partial_doubles with rails 4和rspec 3获取绊倒
我有一个非常简单的视图规格为我的主要指标:
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 =真”在我的配置那么这会导致大量的令人印象深刻的错误,因为它转储整个对象,然后说在底部:
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,但这样做此休息。我把从文档把话说清楚:
如果方法出现在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提供保护,我怎么能解决此问题?
Thanks for th答案。 – 2014-09-19 20:18:49
辉煌。这非常有帮助。谢谢 – GhostRider 2015-05-19 16:08:01