Skip to content

Instantly share code, notes, and snippets.

@akiellor
Created January 2, 2012 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiellor/1549451 to your computer and use it in GitHub Desktop.
Save akiellor/1549451 to your computer and use it in GitHub Desktop.
XPath matcher using nokogiri.
require 'nokogiri'
RSpec::Matchers.define :have_xpath do |*paths|
match do |doc|
!doc.xpath(*paths).empty?
end
end
def within *paths, &block
describe "within #{paths.inspect}" do
self.class.class_eval do
define_method :subject do
super().xpath *paths
end
end
end
end
describe 'some document' do
subject { Nokogiri.XML('<foo><bar /><foo>') }
it { should have_xpath '//bar' }
it { should_not have_xpath '//baz' }
within '//foo' do
it { should have_xpath '//bar' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment