0

我有两个没有类的方法,它们都有相同的代码,但返回对象不同。

def get_load_balancer(network_client, resource_group_name, load_balancer_name)
  promise = network_client.load_balancers.get(resource_group_name, load_balancer_name)
  load_balancer = promise.value!
  return load_balancer.body
end

def get_public_ip(network_client, resource_group_name, public_ip_name)
  promise = network_client.public_ip_addresses.get(resource_group_name, public_ip_name)
  public_ip = promise.value!
  return public_ip.body
end

我试图嘲笑它

@network_client = NetworkResourceProviderClient.new(token)

@promise_lb = Concurrent::Promise.new
allow(@network_client).to receive_message_chain(:load_balancers, :get).and_return(@promise_lb)
allow(@promise_lb).to receive(:value!).and_return(response_lb)

@promise_pip = Concurrent::Promise.new
allow(@network_client).to receive_message_chain(:public_ip_addresses, :get).and_return(@promise_pip)
allow(@promise_pip).to receive(:value!).and_return(response_pip)

问题是,在模拟中传递对象不起作用。请帮我解决这个问题。

4

1 回答 1

0

你为什么用Concurrent::Promise?ChefSpec 是一个 RSpec 库,您将使用该rspec-mocks系统。

before do
  network_client = instance_double(NetworkResourceProviderClient)
  lbs = double()
  allow(lbs).to receive(:get).with('foo', 'bar').and_return(double(value!: 'baz'))
  allow(network_client).to receive(:load_balancers).and_return(lbs)
end
于 2016-12-07T13:05:51.273 回答