1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00
httparty/spec/support/stub_response.rb
Michael Stock cc0c2c85ca Convert specs to RSpec 3.1.7 syntax with Transpec
This conversion is done by Transpec 2.3.8 with the following command:
    transpec -f spec

* 328 conversions
    from: obj.should
      to: expect(obj).to

* 238 conversions
    from: == expected
      to: eq(expected)

* 42 conversions
    from: obj.should_receive(:message)
      to: expect(obj).to receive(:message)

* 26 conversions
    from: it { should ... }
      to: it { is_expected.to ... }

* 26 conversions
    from: lambda { }.should
      to: expect { }.to

* 21 conversions
    from: mock('something')
      to: double('something')

* 21 conversions
    from: obj.stub(:message => value)
      to: allow(obj).to receive_messages(:message => value)

* 19 conversions
    from: be_true
      to: be_truthy

* 19 conversions
    from: its(:attr) { }
      to: describe '#attr' do subject { super().attr }; it { } end

* 18 conversions
    from: obj.stub!(:message)
      to: allow(obj).to receive(:message)

* 15 conversions
    from: obj.should_not_receive(:message)
      to: expect(obj).not_to receive(:message)

* 10 conversions
    from: obj.should_not
      to: expect(obj).not_to

* 7 conversions
    from: obj.stub(:message)
      to: allow(obj).to receive(:message)

* 6 conversions
    from: stub('something')
      to: double('something')

* 5 conversions
    from: be_false
      to: be_falsey

* 4 conversions
    from: expect { }.not_to raise_error(SpecificErrorClass)
      to: expect { }.not_to raise_error

* 4 conversions
    from: lambda { }.should_not
      to: expect { }.not_to

* 3 conversions
    from: =~ /pattern/
      to: match(/pattern/)

* 2 conversions
    from: obj.stub_chain(:message1, :message2)
      to: allow(obj).to receive_message_chain(:message1, :message2)

* 1 conversion
    from: === expected
      to: be === expected

* 1 conversion
    from: it { should_not ... }
      to: it { is_expected.not_to ... }

For more details: https://github.com/yujinakayama/transpec#supported-conversions
2014-12-06 16:12:39 -08:00

43 lines
1.6 KiB
Ruby

module HTTParty
module StubResponse
def stub_http_response_with(filename)
format = filename.split('.').last.intern
data = file_fixture(filename)
response = Net::HTTPOK.new("1.1", 200, "Content for you")
allow(response).to receive(:body).and_return(data)
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', format: format)
allow(http_request).to receive_message_chain(:http, :request).and_return(response)
expect(HTTParty::Request).to receive(:new).and_return(http_request)
end
def stub_chunked_http_response_with(chunks, options={format: "html"})
response = Net::HTTPResponse.new("1.1", 200, nil)
allow(response).to receive(:chunked_data).and_return(chunks)
def response.read_body(&block)
@body || chunked_data.each(&block)
end
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', options)
allow(http_request).to receive_message_chain(:http, :request).and_yield(response).and_return(response)
expect(HTTParty::Request).to receive(:new).and_return(http_request)
end
def stub_response(body, code = 200)
@request.options[:base_uri] ||= 'http://localhost'
unless defined?(@http) && @http
@http = Net::HTTP.new('localhost', 80)
allow(@request).to receive(:http).and_return(@http)
end
response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
allow(response).to receive(:body).and_return(body)
allow(@http).to receive(:request).and_return(response)
response
end
end
end