In my example, I have a service that calls OpenURI's open
, which accepts a block as one of its arguments:
class MyService
def call_response
open("http://website.com/api", http_basic_authentication: ["username","password"], &:read)
end
end
In this case, that last argument calls .read
on the response automatically to get to its body. Now I'd like to test the following:
open
&:read
You can do both very simply all in one spec with Rspec:
require 'spec_helper'
describe MyService do
describe "#open"
it "calls read on response" do
my_service = MyService.new
expect(my_service).to receive(:open) do |url, authentication, &block|
expect(block).to eq :read.to_proc
end
my_service.call_response
end
end
end