我使用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