0

I have two methods without class, they both have same code but return object is different.

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

I am trying to mock it like

@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)

Problem is, passing object in mocking isn't working. please help me with this issue.

1 Answers1

0

Why are you using Concurrent::Promise? ChefSpec is an RSpec library, you would use the rspec-mocks system.

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
coderanger
  • 52,400
  • 4
  • 52
  • 75