Commit 7c97171

mo <mo.khan@gmail.com>
2017-11-12 21:53:42
extract rspec matcher.
1 parent d543c41
Changed files (3)
spec/saml/logout_request_spec.rb
@@ -23,8 +23,7 @@ RSpec.describe Saml::Kit::LogoutRequest do
 
       expect(xml_hash['LogoutRequest']['Issuer']).to eql(subject.issuer)
       expect(xml_hash['LogoutRequest']['NameID']).to eql(name_id)
-      doc = Nokogiri::XML(result)
-      expect(doc.xpath("//LogoutRequest//NameID[@Format=\"#{subject.name_id_format}\"]")).to be_present
+      expect(result).to have_xpath("//LogoutRequest//NameID[@Format=\"#{subject.name_id_format}\"]")
     end
   end
 end
spec/support/matchers/have_xml.rb
@@ -0,0 +1,21 @@
+RSpec::Matchers.define :have_xpath do |xpath|
+  match do |actual|
+    xml_document(actual).xpath(xpath).any?
+  end
+
+  failure_message do |actual|
+    "Expected xpath: #{xpath.inspect} to match in:\n #{xml_pretty_print(actual)}"
+  end
+
+  failure_message_when_negated do |actual|
+    "Expected xpath: #{xpath.inspect} not to match in:\n #{xml_pretty_print(actual)}"
+  end
+
+  def xml_pretty_print(raw_xml)
+    xml_document(raw_xml).to_xml(indent: 2)
+  end
+
+  def xml_document(raw_xml)
+    Nokogiri::XML(raw_xml)
+  end
+end
spec/spec_helper.rb
@@ -4,6 +4,7 @@ require "active_support/testing/time_helpers"
 require "ffaker"
 require "webmock/rspec"
 
+Dir[File.join(Dir.pwd, 'spec/support/**/*.rb')].each { |f| require f }
 RSpec.configure do |config|
   config.include ActiveSupport::Testing::TimeHelpers
   # Enable flags like --only-failures and --next-failure