我正在进行有以下通话的测试。
class Device < ActiveRecord::Base
before_save :notify_provision_server
def notify_provision_server
if provisioned?
return make_api_call(:is_provision,:post, '/api/v1/ivydevice', {
fqdn: site.dns_name ,
customer_id: site.customer_id.to_s,
mac: mac_address,
user_id: login,
password: password,
name: name,
system: SYSTEM_NAME,
timezone: utc_offset_sec.to_s
}.to_json)
else
return make_api_call(:is_provision, :delete, "/api/v1/ivydevices/#{mac_address_was}")
end
end
end
我正在寻找使用mocha存根存根make_api_call方法。由于我没有实例。
Device.any_instance.stubs(:make_api_call).with do |*args|
args[0] == :is_provision
args[1] == :post
## Intentionally do not match the argument.
args[2] == '/api/v1/ivydeviceaaaaaa'
args[3].class == String
end.returns(true).times(1)
显然,参数检查似乎失败了,并且断言成功了
这是测试的样子
_device = create_device(@site, 'test', name: 'Stuff')
Device.any_instance.stubs(:make_api_call).with do |*args|
args[0] == :is_provision
args[1] == :post
args[2] == '/api/v1/ivydeviceaaaaaa'
args[3].class == String
end.returns(true).times(1)
Device.any_instance.stubs(:make_api_call).with do |*args|
args[0] == :is_provision
args[1] == :delete
args[2].class == String
args[3].class == String
end.returns(true).times(0)
## Make the update request
put(
customer_device_url(_device.id),
headers: request_headers,
params: {
data: {
attributes: {
is_provision: true,
mac_address: 'abc:def',
timezone: 'Asia/Kolkata'
}
}
}
)
assert_successful_response