我有一些看起来像这样的代码:
while response.droplet.status != env["user_droplet_desired_state"] do
sleep 2
response = ocean.droplet.show env["droplet_id"]
say ".", nil, false
end
您可以将应用程序设置为等到服务器处于某种状态(例如重新启动它,然后再观察它直到它再次激活)
但是,我在测试中使用了webmock,并且我无法找到第二次给出不同响应的方法。
例如,代码如下:
stub_request(:get, "https://api.digitalocean.com/v2/droplets/6918990?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplet_inactive'), :headers => {})
stub_request(:get, "https://api.digitalocean.com/v2/droplets/6918990?per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplet'), :headers => {})
这个想法是"第一次标记为无效,所以循环经历一次,然后标记为活动后#34;
The documentation says that stubs are just done as "Last one found will work":
将始终应用与请求匹配的最后声明的存根 即:
stub_request(:get," www.example.com")。to_return(:body =>" abc")
stub_request(:get," www.example.com")。to_return(:body =>" def")
Net :: HTTP.get(' www.example.com',' /')#====> " DEF"
是否可以在webmock中为具有不同结果的同一端点建模多个调用?
答案 0 :(得分:17)
如果将哈希数组传递给#to_return
,它将每次响应下一个响应(然后只是反复返回最后一个)。例如:
require 'webmock/rspec'
require 'uri'
describe "something" do
it "happens" do
stub_request(:get, 'example.com/blah').
to_return({status: 200, body: 'ohai'}, {status: 200, body: 'there'})
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
puts Net::HTTP.get(URI('http://example.com/blah'))
end
end
以rspec <file>
运行时,会打印:
ohai
there
there
there