lib/soaspec/matchers.rb in soaspec-0.2.21 vs lib/soaspec/matchers.rb in soaspec-0.2.22
- old
+ new
@@ -69,13 +69,20 @@
end
end
# Whether response has successful status code and correct mandatory elements and values
RSpec::Matchers.define :be_successful do
- match do |actual|
+ # @param [Exchange, RestClient::Response] actual Object that returns Exchange or is Exchange
+ # @return [Exchange] Exchange to use
+ def exchange_from(actual)
+ actual.respond_to?(:exchange) ? actual.exchange : actual
+ end
+
+ # @param [Exchange, RestClient::Response] exchange Object that returns Exchange or is Exchange
+ # @return [Array] List of errors when checking Exchange response is successful
+ def collect_errors(exchange)
failure_list = []
- exchange = actual.respond_to?(:exchange) ? actual.exchange : actual
failure_list << "#{exchange.status_code} not valid status code" unless (200..299).cover?(exchange.status_code)
exchange.exchange_handler.expected_mandatory_elements.each do |mandatory_element_path|
begin
exchange[mandatory_element_path]
rescue NoElementAtPath => error
@@ -86,10 +93,22 @@
failure_list << "Expected value at xpath '#{path}' to be '#{value}' but was '#{exchange[path]}'" unless exchange[path] == value
end
exchange.exchange_handler.expected_mandatory_json_values.each do |path, value|
failure_list << "Expected value at json '#{path}' to be '#{value}' but was '#{exchange[path]}'" unless exchange[path] == value
end
+ failure_list
+ end
+
+ match do |actual|
+ exchange = exchange_from actual
+ failure_list = collect_errors exchange
raise failure_list.to_s unless failure_list.empty?
+ true
+ end
+ match_when_negated do |actual|
+ exchange = exchange_from actual
+ failure_list = collect_errors exchange
+ raise "Expected failure. Status code is #{exchange.status_code}" if failure_list.empty?
true
end
end