spec/unit/pacto/stubs/webmock_adapter_spec.rb in pacto-0.3.1 vs spec/unit/pacto/stubs/webmock_adapter_spec.rb in pacto-0.4.0.rc1

- old
+ new

@@ -1,203 +1,153 @@ +# -*- encoding : utf-8 -*- module Pacto module Stubs + # FIXME: Review this test and see which requests are Pacto vs WebMock, then use Fabricate describe WebMockAdapter do + let(:middleware) { double('middleware') } + let(:request) do - double( - :host => 'http://localhost', - :method => method, - :path => '/hello_world', - :headers => {'Accept' => 'application/json'}, - :params => {'foo' => 'bar'} + Fabricate(:request_clause, + host: 'http://localhost', + http_method: http_method, + path: '/hello_world', + headers: { 'Accept' => 'application/json' }, + params: { 'foo' => 'bar' } ) end - let(:method) { :get } + let(:http_method) { :get } let(:response) do - double( - :status => 200, - :headers => {}, - :body => body + Fabricate( + :response_clause, + status: 200, + headers: {}, + schema: { + type: 'object', + required: ['message'], + properties: { + message: { + type: 'string', + default: 'foo' + } + } + } ) end + let(:contract) do + Fabricate(:contract, request: request, response: response) + end + let(:body) do - {'message' => 'foo'} + { 'message' => 'foo' } end let(:stubbed_request) do { - :path => nil + path: nil } end let(:request_pattern) { double('request_pattern') } - subject(:built_in) { WebMockAdapter.new } + subject(:adapter) { WebMockAdapter.new middleware } before(:each) do - stubbed_request.stub(:to_return).with( - :status => response.status, - :headers => response.headers, - :body => response.body.to_json - ) - stubbed_request.stub(:request_pattern).and_return request_pattern + allow(stubbed_request).to receive(:to_return).with(no_args) + allow(stubbed_request).to receive(:request_pattern).and_return request_pattern end describe '#initialize' do it 'sets up a hook' do - WebMock.should_receive(:after_request) do | arg, &block | - expect(block.parameters).to have(2).items + expect(WebMock).to receive(:after_request) do | _arg, &block | + expect(block.parameters.size).to eq(2) end - # WebMockAdapter.new - Pacto.configuration.provider # this way the rpec after block doesn't create a second instance + Pacto.configuration.adapter # (rather than WebMockAdapter.new, to prevent rpec after block from creating a second instance end end describe '#process_hooks' do let(:request_signature) { double('request_signature') } - before do - Pacto.configuration.hook.stub(:process) + it 'calls the middleware for processing' do + expect(middleware).to receive(:process).with(a_kind_of(Pacto::PactoRequest), a_kind_of(Pacto::PactoResponse)) + adapter.process_hooks request_signature, response end - - it 'calls the registered hook' do - Pacto.configuration.hook.should_receive(:process) - .with(anything, request_signature, response) - built_in.process_hooks request_signature, response - end - - it 'calls generate when generate is enabled' do - Pacto.generate! - WebMockHelper.should_receive(:generate).with(request_signature, response) - built_in.process_hooks request_signature, response - end - - it 'calls validate when validate mode is enabled' do - Pacto.validate! - WebMockHelper.should_receive(:validate).with(request_signature, response) - built_in.process_hooks request_signature, response - end end describe '#stub_request!' do before(:each) do - WebMock.should_receive(:stub_request) do | method, url | + expect(WebMock).to receive(:stub_request) do | _method, url | stubbed_request[:path] = url stubbed_request end end context 'when the response body is an object' do let(:body) do - {'message' => 'foo'} + { 'message' => 'foo' } end - it 'stubs the response body with a json representation' do - stubbed_request.should_receive(:to_return).with( - :status => response.status, - :headers => response.headers, - :body => response.body.to_json - ) - - request_pattern.stub(:with) - - built_in.stub_request! request, response - end - - context 'when the response body is an array' do - let(:body) do - [1, 2, 3] - end - - it 'stubs the response body with a json representation' do - stubbed_request.should_receive(:to_return).with( - :status => response.status, - :headers => response.headers, - :body => response.body.to_json - ) - - request_pattern.stub(:with) - - built_in.stub_request! request, response - end - end - - context 'when the response body is not an object or an array' do - let(:body) { nil } - - it 'stubs the response body with the original body' do - stubbed_request.should_receive(:to_return).with( - :status => response.status, - :headers => response.headers, - :body => response.body - ) - - request_pattern.stub(:with) - - built_in.stub_request! request, response - end - end - context 'a GET request' do - let(:method) { :get } + let(:http_method) { :get } it 'uses WebMock to stub the request' do - request_pattern.should_receive(:with). - with(:headers => request.headers, :query => request.params). + expect(request_pattern).to receive(:with). + with(headers: request.headers, query: request.params). and_return(stubbed_request) - built_in.stub_request! request, response + adapter.stub_request! contract end end context 'a POST request' do - let(:method) { :post } + let(:http_method) { :post } it 'uses WebMock to stub the request' do - request_pattern.should_receive(:with). - with(:headers => request.headers, :body => request.params). + expect(request_pattern).to receive(:with). + with(headers: request.headers, body: request.params). and_return(stubbed_request) - built_in.stub_request! request, response + adapter.stub_request! contract end end context 'a request with no headers' do let(:request) do - double( - :host => 'http://localhost', - :method => :get, - :path => '/hello_world', - :headers => {}, - :params => {'foo' => 'bar'} + Fabricate(:request_clause, + host: 'http://localhost', + http_method: :get, + path: '/hello_world', + headers: {}, + params: { 'foo' => 'bar' } ) end it 'uses WebMock to stub the request' do - request_pattern.should_receive(:with). - with(:query => request.params). + expect(request_pattern).to receive(:with). + with(query: request.params). and_return(stubbed_request) - built_in.stub_request! request, response + adapter.stub_request! contract end end context 'a request with no params' do let(:request) do - double( - :host => 'http://localhost', - :method => :get, - :path => '/hello_world', - :headers => {}, - :params => {} + Fabricate(:request_clause, + host: 'http://localhost', + http_method: :get, + path: '/hello_world', + headers: {}, + params: {} ) end it 'uses WebMock to stub the request' do - request_pattern.should_receive(:with). + expect(request_pattern).to receive(:with). with({}). and_return(stubbed_request) - built_in.stub_request! request, response + adapter.stub_request! contract end end end end end