我需要在Rspec/capybara請求規範中存根current_user
方法的響應。該方法在ApplicationController
中定義,並使用helper_method。該方法應該簡單地返回一個用戶ID。在測試中,我希望這種方法每次都返回相同的用戶ID。如何在請求規範中存儲ApplicationController方法
或者,我可以通過在規範中設置session[:user_id]
來解決我的問題(這是current_user
返回的內容)......但這似乎也不起作用。
這些都可能嗎?
編輯:
這裏是我有(它不工作它只運行正常CURRENT_USER方法)。
require 'spec_helper'
describe "Login" do
before(:each) do
ApplicationController.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
而且不工作:
require 'spec_helper'
describe "Login" do
before(:each) do
@mock_controller = mock("ApplicationController")
@mock_controller.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
是'current_user'一個類的方法描述磕碰的視圖的方法去解決呢?我覺得不是。因此,您可能需要'ApplicationController.any_instance.stub(:current_user)'而不是'ApplicationController.stub(:current_user)'。 – skalee