有时会在给定参数+摩卡失败的情况下调用存根方法

时间:2019-01-23 12:04:31

标签: mocha minitest

我正在进行有以下通话的测试。

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

我在这里缺少什么,因为大多数链接herehere都提到了 with 块的声明,就像我拥有的​​那样。

0 个答案:

没有答案