我有这个简单的代码,其中我发送http请求和读取所有的响应。以下是我的rails代码
open("http://stackoverflow.com/questions/ask")我该如何为这行代码编写规范呢?我没有选择使用mocha和webmock。我只能使用Rpsec的mocking框架。
我试着用这句话
OpenURI.stub!(:open_uri).should_receive(:open).with("http://stackoverflow.com/questions/ask")但是我一直收到这个错误
RSpec::Mocks::MockExpectationError: (#<RSpec::Mocks::MessageExpectation:0xd1a7914>).open("http://stackoverflow.com/questions/ask")
expected: 1 time
received: 0 times发布于 2012-06-29 16:41:09
我以为open方法是在Kernel级别定义的,但我错了。
如果你想模拟open,你应该在你的object的级别上这样做:
it "should do something" do
object_under_test = ObjectUnderTest.new
object_under_test.should_receive(:open).with("http://example.org")
end发布于 2013-01-19 01:06:21
根据此链接,http://distillations.2rye.com/2011/08/mock-the-web-openuri/函数在内核模块上定义,但混合到您的控制器中。因此,您需要在该级别上对调用进行存根。此解决方案适用于RSpec控制器测试:
html_content = <<-EOS
<html><head>
<title>Some Title</title>
</head>
<body>Some Content</body></html>
EOS
YourController.any_instance.stub(:open).and_return html_content发布于 2013-10-16 04:48:50
我做到了:
my_object.stub_chain(:open, :read) { "my return value" }https://stackoverflow.com/questions/10796986
复制相似问题