是的,你可以嘲笑的请求。我在这里描述了如何做到这一点,但实际上这并不一定就是你想要的。
只需在示例中的助手对象上调用您的助手方法。像这样:
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)
如果您的模拟收到messa,Rspec会抱怨ges,你不存根。如果还有其他的东西只要在助手对象上调用你的请求帮助器方法就是在测试中你不关心,你可以通过使模拟成为“空对象”来关闭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
你有“需要“的RSpec/Rails的”在你的spec_helper.rb? – Rubyman
是的,我喜欢。谢谢。 –