Added assert_xpath and assert_no_xpath for Test::Unit support.

This commit is contained in:
Amos King 2009-01-05 12:23:48 -06:00
parent fece459f5f
commit 4e0845c8db
2 changed files with 37 additions and 0 deletions

View File

@ -79,5 +79,15 @@ module Webrat
end end
alias_method :match_xpath, :have_xpath alias_method :match_xpath, :have_xpath
def assert_xpath(expected, &block)
hs = HaveXpath.new(expected, &block)
raise Test::Unit::AssertionFailedError.new(hs.failure_message) unless hs.matches?(response_body)
end
def assert_no_xpath(expected, &block)
hs = HaveXpath.new(expected, &block)
raise Test::Unit::AssertionFailedError.new(hs.negative_failure_message) if hs.matches?(response_body)
end
end end
end end

View File

@ -54,6 +54,33 @@ describe Webrat::Matchers do
}.should raise_error(Spec::Expectations::ExpectationNotMetError) }.should raise_error(Spec::Expectations::ExpectationNotMetError)
end end
describe 'asserts for xpath' do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_xpath" do
it "should pass when body contains the selection" do
assert_xpath("//div")
end
it "should throw an exception when the body doesnt have matching xpath" do
lambda {assert_xpath("//p")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_no_xpath" do
it "should pass when the body doesn't contan the xpath" do
assert_no_xpath("//p")
end
it "should throw an exception when the body does contain the xpath" do
lambda {assert_no_xpath("//div")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
end end
describe "#have_selector" do describe "#have_selector" do