lib/soaspec/matchers.rb in soaspec-0.1.18 vs lib/soaspec/matchers.rb in soaspec-0.2.0
- old
+ new
@@ -60,7 +60,30 @@
end
failure_message do |exchange|
"expected result #{exchange.response} to be found. Status code is #{exchange.response.code}"
end
+end
-end
\ No newline at end of file
+# Whether response has successful status code and correct mandatory elements and values
+RSpec::Matchers.define :be_successful do
+ match do |actual|
+ 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
+ failure_list << error.message
+ end
+ end
+ exchange.exchange_handler.expected_mandatory_xpath_values.each do |path, value|
+ 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
+ raise failure_list.to_s unless failure_list.empty?
+ true
+ end
+end