lib/pacto/rspec.rb in pacto-0.3.1 vs lib/pacto/rspec.rb in pacto-0.4.0.rc1

- old
+ new

@@ -1,100 +1,107 @@ +# -*- encoding : utf-8 -*- require 'pacto' begin require 'rspec/core' require 'rspec/expectations' rescue LoadError raise 'pacto/rspec requires rspec 2 or later' end -RSpec::Matchers.define :have_unmatched_requests do |method, uri| - @unmatched_validations = Pacto::ValidationRegistry.instance.unmatched_validations +require 'pacto/forensics/investigation_filter' +require 'pacto/forensics/investigation_matcher' + +RSpec::Matchers.define :have_unmatched_requests do |_method, _uri| match do - !@unmatched_validations.empty? + @unmatched_investigations = Pacto::InvestigationRegistry.instance.unmatched_investigations + !@unmatched_investigations.empty? end - failure_message_for_should do + failure_message do 'Expected Pacto to have not matched all requests to a Contract, but all requests were matched.' end - failure_message_for_should_not do - unmatched_requests = @unmatched_validations.map(&:request).join("\n ") + failure_message_when_negated do + unmatched_requests = @unmatched_investigations.map(&:request).join("\n ") "Expected Pacto to have matched all requests to a Contract, but the following requests were not matched: \n #{unmatched_requests}" end end -RSpec::Matchers.define :have_failed_validations do |method, uri| - @failed_validations = Pacto::ValidationRegistry.instance.failed_validations +RSpec::Matchers.define :have_failed_investigations do |_method, _uri| match do - !@failed_validations.empty? + @failed_investigations = Pacto::InvestigationRegistry.instance.failed_investigations + !@failed_investigations.empty? end - failure_message_for_should do - 'Expected Pacto to have found validation problems, but none were found.' + failure_message do + 'Expected Pacto to have found investigation problems, but none were found.' end - failure_message_for_should_not do - "Expected Pacto to have successfully validated all requests, but the following issues were found: #{@failed_validations}" + failure_message_when_negated do + "Expected Pacto to have successfully validated all requests, but the following issues were found: #{@failed_investigations}" end end RSpec::Matchers.define :have_validated do |method, uri| - @request_pattern = WebMock::RequestPattern.new(method, uri) match do + @request_pattern = Pacto::RequestPattern.new(method, uri) + @request_pattern.with(@options) if @options validated? @request_pattern end chain :against_contract do |contract| @contract = contract end chain :with do |options| - @request_pattern.with options + @options = options end - def validated?(request_pattern) - @matching_validations = Pacto::ValidationRegistry.instance.validated? @request_pattern - validated = !@matching_validations.nil? + def validated?(_request_pattern) + @matching_investigations = Pacto::InvestigationRegistry.instance.validated? @request_pattern + validated = !@matching_investigations.nil? validated && successfully? && contract_matches? end - def validation_results - @validation_results ||= @matching_validations.map(&:results).flatten.compact + def investigation_citations + @investigation_citations ||= @matching_investigations.map(&:citations).flatten.compact end def successfully? - @matching_validations.map(&:successful?).uniq.eql? [true] + @matching_investigations.map(&:successful?).uniq.eql? [true] end def contract_matches? if @contract - validated_contracts = @matching_validations.map(&:contract) + validated_contracts = @matching_investigations.map(&:contract).compact # Is there a better option than case equality for string & regex support? - validated_contracts.map(&:file).index { |file| @contract === file } # rubocop:disable CaseEquality + validated_contracts.any? do |contract| + @contract === contract.file || @contract === contract.name # rubocop:disable CaseEquality + end else true end end - failure_message_for_should do + failure_message do buffer = StringIO.new buffer.puts "expected Pacto to have validated #{@request_pattern}" - if @matching_validations.nil? || @matching_validations.empty? + if @matching_investigations.nil? || @matching_investigations.empty? buffer.puts ' but no matching request was received' buffer.puts ' received:' buffer.puts "#{WebMock::RequestRegistry.instance}" - elsif @matching_validations.map(&:contract).compact.empty? + elsif @matching_investigations.map(&:contract).compact.empty? buffer.puts ' but a matching Contract was not found' elsif !successfully? - buffer.puts ' but validation errors were found:' + buffer.puts ' but investigation errors were found:' buffer.print ' ' - buffer.puts validation_results.join "\n " - # validation_results.each do |validation_result| - # buffer.puts " #{validation_result}" + buffer.puts investigation_citations.join "\n " + # investigation_citations.each do |investigation_result| + # buffer.puts " #{investigation_result}" # end elsif @contract - validated_against = @matching_validations.map { |v| v.against_contract? @contract }.compact.join ',' + validated_against = @matching_investigations.map { |v| v.against_contract? @contract }.compact.join ',' buffer.puts " against Contract #{@contract}" buffer.puts " but it was validated against #{validated_against}" end buffer.string end