spec/unit/pacto/pacto_spec.rb in pacto-0.2.5 vs spec/unit/pacto/pacto_spec.rb in pacto-0.3.0.pre
- old
+ new
@@ -1,26 +1,44 @@
describe Pacto do
- let(:contract_name) { 'contract' }
- let(:contract) { double('contract') }
- after do
- described_class.unregister_all!
+ around(:each) do |example|
+ $stdout = StringIO.new
+ example.run
+ $stdout = STDOUT
end
- describe '.register' do
- context 'by default' do
- it 'should register a contract under a given name' do
- described_class.register(contract_name, contract)
- described_class.registered[contract_name].should == contract
+ def output
+ $stdout.string.strip
+ end
+
+ def mock_validation(errors)
+ expect(JSON::Validator).to receive(:fully_validate).with(any_args).and_return errors
+ end
+
+ describe '.validate_contract' do
+ context 'valid' do
+ it 'displays a success message and return true' do
+ mock_validation []
+ success = Pacto.validate_contract 'my_contract.json'
+ expect(output).to eq 'All contracts successfully meta-validated'
+ expect(success).to be_true
end
end
- context 'when a contract has already been registered with the same name' do
- it 'should raise an argument error' do
- described_class.register(contract_name, contract)
- expect { described_class.register(contract_name, contract) }.to raise_error(ArgumentError)
+ context 'invalid' do
+ it 'displays one error messages and return false' do
+ mock_validation ['Error 1']
+ success = Pacto.validate_contract 'my_contract.json'
+ expect(output).to match /error/
+ expect(success).to be_false
end
+
+ it 'displays several error messages and return false' do
+ mock_validation ['Error 1', 'Error 2']
+ success = Pacto.validate_contract 'my_contract.json'
+ expect(success).to be_false
+ end
end
end
describe '.build_from_file' do
let(:path) { 'contract/path' }
@@ -33,54 +51,10 @@
described_class.build_from_file(path, host, file_pre_processor)
end
it 'returns whatever the factory returns' do
Pacto::ContractFactory.stub(:build_from_file => instantiated_contract)
- described_class.build_from_file(path, host, file_pre_processor).should == instantiated_contract
+ expect(described_class.build_from_file(path, host, file_pre_processor)).to eq instantiated_contract
end
end
- describe '.use' do
- before do
- described_class.register(contract_name, contract)
- end
-
- context 'by default' do
- let(:instantiated_contract) { double('instantiated contract', :response_body => response_body)}
- let(:response_body) { double('response_body') }
-
- before do
- described_class.registered[contract_name].stub(:instantiate => instantiated_contract)
- instantiated_contract.stub(:stub!)
- end
-
- it 'should instantiate a contract with default values' do
- described_class.registered[contract_name].should_receive(:instantiate).with(nil).and_return(instantiated_contract)
- described_class.use(contract_name)
- end
-
- it 'should return the instantiated contract' do
- described_class.use(contract_name).should == instantiated_contract
- end
-
- it 'should stub further requests with the instantiated contract' do
- instantiated_contract.should_receive(:stub!)
- described_class.use(contract_name)
- end
-
- end
-
- context 'when contract has not been registered' do
- it 'should raise an argument error' do
- expect { described_class.use('unregistered') }.to raise_error ArgumentError
- end
- end
- end
-
- describe '.unregister_all!' do
- it 'should unregister all previously registered contracts' do
- described_class.register(contract_name, contract)
- described_class.unregister_all!
- described_class.registered.should be_empty
- end
- end
end