我有一个RSpec测试,它需要将Request
和HTTPI
类/模块加倍,并返回一个模拟的REST响应.在此方法进行另一次REST调用并需要返回新的REST响应之前,我一直在使用该方法.
API Class
NOTE this is a trimmed down version of the class but the gist is there
class API
include HTTPI
def find_device(id)
req = create_request('path')
req.body = { :customerId => MultiJson.dump(customer_id) }
return call(req)
end
def find_other_device(other_id)
req = create_request('path')
req.body = { :other_id => MultiJson.dump(other_id) }
data = call(req)
return data
end
def call(req)
response = HTTPI.send(req)
return response.body
end
end
装置 文件调用REST方法
class Device
@api = API.new(:open_timeout => 30, :read_timeout => 30)
def get_device
devices = @api.find_device(@id)
log.info("First Call Made")
other_call = @api.find_other_device(@other_id)
end
end
等级库文件
Rspec.describe Device do
resp = {code: 200, body: resp_body, raw_body: "TEST BODY", cookies: [cookie_list_hash]}
resp2 = {code: 200, body: resp_body_2, raw_body: "TEST BODY 2", cookies: [cookie_list_hash]}
let!(:request) {class_double('Request', new: http).as_stubbed_const} # I understand this causes the HTTPI send request to always return the same resp, but without it the test does not even get past the first call
let!(:http) {class_double('HTTPI', send: resp).as_stubbed_const}
it 'test' do
Device.get_device
end
end
我们希望创建一个双精度函数,首先返回resp var,然后在第二次调用:end时返回res2.
我也是Ruby的新手,所以这可能会很难看.