Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock Net::HTTP::Post?

Yes, I know it is best to use webmock, but I would like to know how to mock this method in RSpec:

def method_to_test
  url = URI.parse uri
  req = Net::HTTP::Post.new url.path
  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request req, foo: 1
  end
  res
end

Here is the RSpec:

let( :uri ) { 'http://example.com' }

specify 'HTTP call' do
  http = mock :http
  Net::HTTP.stub!(:start).and_yield http
  http.should_receive(:request).with(Net::HTTP::Post.new(uri), foo: 1)
    .and_return 202
  method_to_test.should == 202
end

The test fails because with seems to be trying to match the NET::HTTP::Post object:

RSpec::Mocks::MockExpectationError: (Mock :http).request(#<Net::HTTP::Post POST>, {:foo=>"1"})
expected: 1 time
received: 0 times

Mock :http received :request with unexpected arguments
     expected: (#<Net::HTTP::Post POST>, {:foo=>"1"})
          got: (#<Net::HTTP::Post POST>, {:foo=>"1"})

How to match properly?

like image 704
B Seven Avatar asked Dec 07 '12 01:12

B Seven


Video Answer


2 Answers

If you don't care about the exact instance, you could use the an_instance_of method:

http.should_receive(:request).with(an_instance_of(Net::HTTP::Post), foo: 1)
.and_return 202
like image 178
Peter Brown Avatar answered Sep 24 '22 08:09

Peter Brown


Here it is with new syntax:

before do
  http = double
  allow(Net::HTTP).to receive(:start).and_yield http
  allow(http).to \
    receive(:request).with(an_instance_of(Net::HTTP::Get))
      .and_return(Net::HTTPResponse)
end

And then in example:

it "http" do
  allow(Net::HTTPResponse).to receive(:body)
    .and_return('the actual body of response')
  # here execute request
end

Very helpful if you need test external api library.

like image 25
Artem P Avatar answered Sep 21 '22 08:09

Artem P