gpt4 book ai didi

ruby - RSpec 如何 stub 打开?

转载 作者:数据小太阳 更新时间:2023-10-29 06:51:54 24 4
gpt4 key购买 nike

我一直在尝试 stub open,open-uri 版本,但我没有成功。

我已尝试执行以下操作,但请求一直在进行:

Kernel.should_receive(:open).and_return("Whatever for now")

我也试过

OpenURI::OpenRead.should_receive(:open).and_return("Whatever for now")

自从我追踪到那是在 OpenURI 中发出 HTTP 请求的地方。

提前感谢您的任何建议!

最佳答案

这是我的做法

class Gateway

def do_something
open('http://example.com').read
end

end

在我的规范中,我做了以下事情:

describe 'communication' do

it 'should receive valid response from example.com' do
gateway = Gateway.new
gateway.stub_chain(:open, :read).and_return('Remote server response')

gateway.do_something.should == "Remote server response"
end

end

关于ruby - RSpec 如何 stub 打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3603256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com