Created
January 2, 2012 05:20
-
-
Save akiellor/1549451 to your computer and use it in GitHub Desktop.
XPath matcher using nokogiri.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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