2009-02-16 00:47:12 +00:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
|
|
|
|
|
|
|
describe "contain" do
|
|
|
|
include Webrat::Matchers
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
before(:each) do
|
|
|
|
@body = <<-EOF
|
|
|
|
<div id='main'>
|
|
|
|
<div class='inner'>hello, world!</div>
|
|
|
|
</div>
|
|
|
|
EOF
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe "#matches?" do
|
|
|
|
it "should call element#contains? when the argument is a string" do
|
|
|
|
@body.should contain("hello, world!")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should call element#matches? when the argument is a regular expression" do
|
|
|
|
@body.should contain(/hello, world/)
|
|
|
|
end
|
2010-01-13 21:42:57 +00:00
|
|
|
|
|
|
|
it "should treat newlines as spaces" do
|
|
|
|
"<div>it takes\ndifferent strokes</div>".should contain("it takes different strokes")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should multiple spaces as a single space" do
|
|
|
|
"<div>it takes different strokes</div>".should contain("it takes different strokes")
|
|
|
|
end
|
2009-02-16 00:47:12 +00:00
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe "asserts for contains," do
|
|
|
|
include Test::Unit::Assertions
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
before(:each) do
|
|
|
|
should_receive(:response_body).and_return @body
|
|
|
|
require 'test/unit'
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe "assert_contain" do
|
|
|
|
it "should pass when containing the text" do
|
|
|
|
assert_contain("hello, world")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should pass when containing the regexp" do
|
|
|
|
assert_contain(/hello, world/)
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should throw an exception when the body doesnt contain the text" do
|
|
|
|
lambda {
|
|
|
|
assert_contain("monkeys")
|
2009-08-12 06:52:01 +00:00
|
|
|
}.should raise_error(AssertionFailedError)
|
2009-02-16 00:47:12 +00:00
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should throw an exception when the body doesnt contain the regexp" do
|
|
|
|
lambda {
|
|
|
|
assert_contain(/monkeys/)
|
2009-08-12 06:52:01 +00:00
|
|
|
}.should raise_error(AssertionFailedError)
|
2009-02-16 00:47:12 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
describe "assert_not_contain" do
|
|
|
|
it "should pass when not containing the text" do
|
|
|
|
assert_not_contain("monkeys")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should pass when not containing the regexp" do
|
|
|
|
assert_not_contain(/monkeys/)
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should throw an exception when the body does contain the text" do
|
|
|
|
lambda {
|
|
|
|
assert_not_contain("hello, world")
|
2009-08-12 06:52:01 +00:00
|
|
|
}.should raise_error(AssertionFailedError)
|
2009-02-16 00:47:12 +00:00
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should throw an exception when the body does contain the regexp" do
|
|
|
|
lambda {
|
|
|
|
assert_not_contain(/hello, world/)
|
2009-08-12 06:52:01 +00:00
|
|
|
}.should raise_error(AssertionFailedError)
|
2009-02-16 00:47:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#failure_message" do
|
|
|
|
it "should include the content string" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new("hello, world!")
|
|
|
|
hc.matches?(@body)
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
hc.failure_message.should include("\"hello, world!\"")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should include the content regular expresson" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
|
|
|
|
hc.matches?(@body)
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
hc.failure_message.should include("/hello,\\sworld!/")
|
|
|
|
end
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
it "should include the element's inner content" do
|
|
|
|
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
|
|
|
|
hc.matches?(@body)
|
2009-04-08 00:30:12 +00:00
|
|
|
|
2009-02-16 00:47:12 +00:00
|
|
|
hc.failure_message.should include("hello, world!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|