added assert_tag, assert_no_tag

This commit is contained in:
gaffo 2009-01-04 17:42:54 -06:00
parent 0f0dab0b0b
commit cf1589823d
2 changed files with 57 additions and 0 deletions

View File

@ -53,5 +53,19 @@ module Webrat
alias_method :match_tag, :have_tag alias_method :match_tag, :have_tag
# Asserts that the body of the response contains
# the supplied tag with the associated selectors
def assert_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
raise Test::Unit::AssertionFailedError.new(ht.failure_message) unless ht.matches?(response_body)
end
# Asserts that the body of the response
# does not contain the supplied string or regepx
def assert_no_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
raise Test::Unit::AssertionFailedError.new(ht.negative_failure_message) if ht.matches?(response_body)
end
end end
end end

View File

@ -133,6 +133,49 @@ describe Webrat::Matchers do
} }
end end
describe "asserts for tags," do
before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
end
describe "assert_tag" do
it "should pass when body contains the tag" do
assert_tag("div")
end
it "should pass when finding with additional selectors" do
assert_tag("div", :class => "inner")
end
it "should throw an exception when the body doesnt have matching tag" do
lambda {assert_tag("p")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body doens't have a tag matching the additional selector" do
lambda {assert_tag("div", :class => "nope")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
describe "assert_no_tag" do
it "should pass when the body doesn't contan the tag" do
assert_no_tag("p")
end
it "should pass when the body doesn't contain the tag due to additional selectors missing" do
assert_no_tag("div", :class => "nope")
end
it "should throw an exception when the body does contain the tag" do
lambda {assert_no_tag("div")}.should raise_error(Test::Unit::AssertionFailedError)
end
it "should throw an exception when the body contains the tag with additional selectors" do
lambda {assert_no_tag("div", :class => "inner")}.should raise_error(Test::Unit::AssertionFailedError)
end
end
end
end end
describe Webrat::Matchers::HasContent do describe Webrat::Matchers::HasContent do