Yield a double to a Block in RSpec
TIL in rspec you can yield a double to a block with and_yield
, similar to how you return a double with and_return
.
With and_return
you can write a test like this:
sftp = Net::SFTP.start(args)
sftp.upload!(content, path)
# Test
client = double
allow(Net::SFTP).to receive(:start).and_return(client)
expect(client).to receive(:upload!)
However, if your code has a block like below and_return
won't work. Instead, you can use and_yield
to yield the double to the block:
Net::SFTP.start(args) do |sftp|
sftp.upload!(content, path)
end
# Test
client = double
allow(Net::SFTP).to receive(:start).and_yield(client)
expect(client).to receive(:upload!)
Tweet