Reputation: 7419
Let's say I am calling a Ruby method with a block argument in ruby:
Net::SFTP.start('testhost.com', 'test_user', keys: ['key']) do |sftp|
sftp.upload!('/local', '/remote')
end
How can I test that the upload!
method was called with the correct arguments?
I could get this far, testing the arguments for #start
,
expect(Net::SFTP).
to receive(:start) do |host, username, keyword_args, &block|
expect(host).to eq("testhost.com")
expect(username).to eq("test_user")
expect(keyword_args).to eq(keys: ["test_key"])
end
But I can't figure out how to test that #upload!
was called in the block.
Upvotes: 5
Views: 2483
Reputation: 44685
Make use of and_yield
- this works best when combined with allow().to receive
and have_received
matcher:
sftp = spy
allow(Net::SFTP).to receive(:start).and_yield(sftp)
# execute your code here
expect(Net::SFTP).to have_received(:start).with("testhost.com", "test_user", keys: ["test_key"])
expect(sftp).to have_received(:upload!).with('./local', '/remote')
Upvotes: 3