spec/unit/pacto/contract_spec.rb in pacto-0.3.0.pre vs spec/unit/pacto/contract_spec.rb in pacto-0.3.0

- old
+ new

@@ -1,78 +1,77 @@ module Pacto describe Contract do let(:request) { double 'request' } - let(:request_signature) { double 'request_signature' } - let(:response) { double 'response' } + let(:response) { double 'response definition' } let(:provider) { double 'provider' } - let(:instantiated_response) { double 'instantiated response' } + let(:file) { 'contranct.json' } + let(:request_pattern_provider) { double(for: nil) } - subject(:contract) { described_class.new request, response } + subject(:contract) { Contract.new(request, response, file, 'sample', request_pattern_provider) } before do - response.stub(:instantiate => instantiated_response) Pacto.configuration.provider = provider end + it 'has a request pattern' do + pattern = double + expect(request_pattern_provider).to receive(:for).and_return(pattern) + expect(contract.request_pattern).to eq pattern + end + describe '#stub_contract!' do - it 'instantiates the response and registers a stub' do - response.should_receive :instantiate - provider.should_receive(:stub_request!).with request, instantiated_response + it 'register a stub for the contract' do + provider.should_receive(:stub_request!).with(request, response) contract.stub_contract! end end - describe '#validate' do + context 'validations' do + let(:fake_response) { double('fake response') } + let(:validation_result) { double 'validation result' } + before do - response.stub(:validate => validation_result) - request.stub(:execute => fake_response) + allow(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}).and_return validation_result end - let(:validation_result) { double 'validation result' } - let(:fake_response) { double 'fake response' } + describe '#validate_consumer' do + it 'returns the result of the validation' do + expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}) + expect(contract.validate_consumer request, fake_response).to eq validation_result + end - it 'validates the generated response' do - response.should_receive(:validate).with(fake_response, {}) - expect(contract.validate).to eq validation_result + it 'does not generate another response' do + request.should_not_receive :execute + contract.validate_consumer request, fake_response + end end - it 'returns the result of the validation' do - expect(contract.validate).to eq validation_result - end + describe '#validate_provider' do + before do + allow(request).to receive(:execute).and_return fake_response + end - it 'generates the response' do - request.should_receive :execute - contract.validate - end + it 'generates the response' do + request.should_receive :execute + contract.validate_provider + end - context 'when response gotten is provided' do - it 'does not generate the response' do - request.should_not_receive :execute - contract.validate fake_response + it 'returns the result of the validating the generated response' do + expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}) + expect(contract.validate_provider).to eq validation_result end end end describe '#matches?' do - let(:request_matcher) do - double('fake request matcher').tap do |matcher| - matcher.stub(:matches?) { |r| r == request_signature } - end - end + let(:request_pattern) { double(matches?: true) } + let(:request_signature) { double } - context 'when the contract is not stubbed' do - it 'returns false' do - expect(contract.matches? request_signature).to be_false - end - end + it 'delegates to the request pattern' do + expect(request_pattern_provider).to receive(:for).and_return(request_pattern) + expect(request_pattern).to receive(:matches?).with(request_signature) - context 'when the contract is stubbed' do - it 'returns true if it matches the request' do - provider.should_receive(:stub_request!).with(request, instantiated_response).and_return(request_matcher) - contract.stub_contract! - expect(contract.matches? request_signature).to be_true - expect(contract.matches? :anything).to be_false - end + contract.matches?(request_signature) end end end end