我需要在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