是的,您可以模拟该请求。我在这里有一个很长的答案,描述了如何做到这一点,但实际上这不一定是您想要的。
只需在示例中的辅助对象上调用辅助方法即可。就像这样:
describe "#item" do
it "does whatever" do
helper.item.should ...
end
end
这将使您能够访问测试请求对象。如果需要为请求路径指定特定值,可以这样做:
before :each do
helper.request.path = 'some-path'
end
实际上,为了完整起见,让我包括我原来的答案,因为根据你想要做什么,它可能仍然有帮助。
以下是模拟请求的方法:
request = mock('request')
controller.stub(:request).and_return request
您可以类似地向返回的请求添加存根方法
request.stub(:method).and_return return_value
以及在一行中模拟和存根的替代语法:
request = mock('request', :method => return_value)
如果你的模拟收到你没有存根的消息,Rspec 会抱怨。如果还有其他事情只需在帮助器对象上调用请求帮助器方法正在执行您在测试中不关心的事情,您可以通过将模拟设置为“空对象”来关闭 rspec,例如。像这样
request = mock('request').as_null_object
看起来您可能需要通过以下特定测试:
describe "#item" do
let(:request){ mock('request', :fullpath => 'some-path') }
before :each do
controller.stub(:request).and_return request
end
it "does whatever"
end