RSpec: how can I spy on interactions with an existing object?

I have an existing stub object, that I am using as a collaborator in my tests:

cf_client = Aws::CloudFormation::Client.new(:stub_responses => true)
stack = described_class.new("name", cf_client)

The stub object (cf_client) was not created using rspec-mocks; it’s a fake implementation provided by the aws-sdk gem, and has useful properties.

However, it doesn’t provide message verification, so I am hoping to wrap in a proxy that records the received messages, so I can verify them later, while still delegating to the wrapped object, e.g.

cf_client = proxy_spy(Aws::CloudFormation::Client.new(:stub_responses => true))
stack = described_class.new("name", cf_client)

stack.create

expect(cf_client).to have_received(:create_stack)

Want I want/need is the proxy_spy method. Does something like this exist? Can I use RSpec’s verifying proxies here?


Source: ruby

Leave a Reply