spec/unit/pacto/contract_spec.rb in pacto-0.3.1 vs spec/unit/pacto/contract_spec.rb in pacto-0.4.0.rc1
- old
+ new
@@ -1,74 +1,86 @@
+# -*- encoding : utf-8 -*-
module Pacto
describe Contract do
- let(:request) { double 'request' }
- let(:response) { double 'response definition' }
- let(:provider) { double 'provider' }
- let(:file) { 'contranct.json' }
- let(:request_pattern_provider) { double(for: nil) }
+ let(:request_clause) do
+ Pacto::RequestClause.new(
+ http_method: 'GET',
+ host: 'http://example.com',
+ path: '/',
+ schema: {
+ type: 'object',
+ required: true # , :properties => double('body definition properties')
+ }
+ )
+ end
+ let(:response_clause) do
+ Pacto::ResponseClause.new(status: 200)
+ end
+ let(:adapter) { double 'provider' }
+ let(:file) { 'contract.json' }
+ let(:consumer_driver) { double }
+ let(:provider_actor) { double }
- subject(:contract) { Contract.new(request, response, file, 'sample', request_pattern_provider) }
+ subject(:contract) do
+ described_class.new(
+ request: request_clause,
+ response: response_clause,
+ file: file,
+ name: 'sample'
+ )
+ end
before do
- Pacto.configuration.provider = provider
+ Pacto.configuration.adapter = adapter
+ allow(consumer_driver).to receive(:respond_to?).with(:execute).and_return true
+ allow(provider_actor).to receive(:respond_to?).with(:build_response).and_return true
+ Pacto.configuration.default_consumer.driver = consumer_driver
+ Pacto.configuration.default_provider.actor = provider_actor
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 'register a stub for the contract' do
- provider.should_receive(:stub_request!).with(request, response)
+ expect(adapter).to receive(:stub_request!).with(contract)
contract.stub_contract!
end
end
- context 'validations' do
- let(:fake_response) { double('fake response') }
- let(:validation_result) { double 'validation result' }
+ context 'investigations' do
+ let(:request) { Pacto.configuration.default_consumer.build_request contract }
+ let(:fake_response) { Fabricate(:pacto_response) } # double('fake response') }
+ let(:cop) { double 'cop' }
+ let(:investigation_citations) { [double('investigation result')] }
before do
- allow(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}).and_return validation_result
+ Pacto::Cops.active_cops.clear
+ Pacto::Cops.active_cops << cop
+ allow(cop).to receive(:investigate).with(an_instance_of(Pacto::PactoRequest), fake_response, contract).and_return investigation_citations
end
- 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 'does not generate another response' do
- request.should_not_receive :execute
- contract.validate_consumer request, fake_response
- end
- end
-
- describe '#validate_provider' do
+ describe '#simulate_request' do
before do
- allow(request).to receive(:execute).and_return fake_response
+ allow(consumer_driver).to receive(:execute).with(an_instance_of(Pacto::PactoRequest)).and_return fake_response
end
it 'generates the response' do
- request.should_receive :execute
- contract.validate_provider
+ expect(consumer_driver).to receive(:execute).with(an_instance_of(Pacto::PactoRequest))
+ contract.simulate_request
end
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
+ expect(cop).to receive(:investigate).with(an_instance_of(Pacto::PactoRequest), fake_response, contract)
+ investigation = contract.simulate_request
+ expect(investigation.citations).to eq investigation_citations
end
end
end
describe '#matches?' do
- let(:request_pattern) { double(matches?: true) }
- let(:request_signature) { double }
+ let(:request_pattern) { double('request_pattern') }
+ let(:request_signature) { double('request_signature') }
it 'delegates to the request pattern' do
- expect(request_pattern_provider).to receive(:for).and_return(request_pattern)
+ expect(Pacto::RequestPattern).to receive(:for).and_return(request_pattern)
expect(request_pattern).to receive(:matches?).with(request_signature)
contract.matches?(request_signature)
end
end