Add should_see and should_not_see assertions
This commit is contained in:
parent
44db244a76
commit
1a4db57d93
2
Rakefile
2
Rakefile
@ -56,7 +56,7 @@ end
|
||||
|
||||
require 'spec/rake/verify_rcov'
|
||||
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
|
||||
t.threshold = 97.1 # Make sure you have rcov 0.7 or higher!
|
||||
t.threshold = 97.2 # Make sure you have rcov 0.7 or higher!
|
||||
end
|
||||
|
||||
remove_task "default"
|
||||
|
27
lib/webrat/core/assertions.rb
Normal file
27
lib/webrat/core/assertions.rb
Normal file
@ -0,0 +1,27 @@
|
||||
module Webrat
|
||||
module Assertions
|
||||
|
||||
def should_see(text_or_regexp)
|
||||
case text_or_regexp
|
||||
when Regexp
|
||||
return if scoped_html.match(text_or_regexp)
|
||||
else
|
||||
return if scoped_html.include?(text_or_regexp)
|
||||
end
|
||||
|
||||
flunk("Should see #{text_or_regexp.inspect} but didn't")
|
||||
end
|
||||
|
||||
def should_not_see(text_or_regexp)
|
||||
case text_or_regexp
|
||||
when Regexp
|
||||
return unless scoped_html.match(text_or_regexp)
|
||||
else
|
||||
return unless scoped_html.include?(text_or_regexp)
|
||||
end
|
||||
|
||||
flunk("Should not see #{text_or_regexp.inspect} but did")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -4,6 +4,7 @@ module Webrat
|
||||
class Scope
|
||||
include Logging
|
||||
include Flunk
|
||||
include Assertions
|
||||
|
||||
def initialize(session, html, selector = nil)
|
||||
@session = session
|
||||
|
@ -141,5 +141,7 @@ module Webrat
|
||||
def_delegators :current_scope, :click_post_link, :clicks_post_link
|
||||
def_delegators :current_scope, :click_put_link, :clicks_put_link
|
||||
def_delegators :current_scope, :click_button, :clicks_button
|
||||
def_delegators :current_scope, :should_see
|
||||
def_delegators :current_scope, :should_not_see
|
||||
end
|
||||
end
|
73
spec/api/should_not_see_spec.rb
Normal file
73
spec/api/should_not_see_spec.rb
Normal file
@ -0,0 +1,73 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
||||
|
||||
describe "should_not_see" do
|
||||
before do
|
||||
@session = Webrat::TestSession.new
|
||||
end
|
||||
|
||||
it "should fail if the string is in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.should_not_see "Link"
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
it "should fail if the regexp is in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.should_not_see /Li(n)[ck]/
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
it "should fail if the string is in the HTML scope" do
|
||||
@session.response_body = <<-EOS
|
||||
<div id="first">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
<div id="second">
|
||||
</div>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.within "#first" do |scope|
|
||||
scope.should_not_see "Link"
|
||||
end
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
it "should pass if the string is not in the HTML scope" do
|
||||
@session.response_body = <<-EOS
|
||||
<div id="first">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
<div id="second">
|
||||
</div>
|
||||
EOS
|
||||
|
||||
@session.within "#second" do |scope|
|
||||
scope.should_not_see "Link"
|
||||
end
|
||||
end
|
||||
|
||||
it "should pass if the string is not in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
@session.should_not_see "Missing"
|
||||
end
|
||||
|
||||
it "should pass if the regexp is not in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Different</a>
|
||||
EOS
|
||||
|
||||
@session.should_not_see /Li(n)[ck]/
|
||||
end
|
||||
end
|
73
spec/api/should_see_spec.rb
Normal file
73
spec/api/should_see_spec.rb
Normal file
@ -0,0 +1,73 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
||||
|
||||
describe "should_see" do
|
||||
before do
|
||||
@session = Webrat::TestSession.new
|
||||
end
|
||||
|
||||
it "should pass if the string is in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
@session.should_see "Link"
|
||||
end
|
||||
|
||||
it "should pass if the regexp is in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
@session.should_see /Li(n)[ck]/
|
||||
end
|
||||
|
||||
it "should pass if the string is in the HTML scope" do
|
||||
@session.response_body = <<-EOS
|
||||
<div id="first">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
<div id="second">
|
||||
</div>
|
||||
EOS
|
||||
|
||||
@session.within "#first" do |scope|
|
||||
scope.should_see "Link"
|
||||
end
|
||||
end
|
||||
|
||||
it "should fail if the string is not in the HTML scope" do
|
||||
@session.response_body = <<-EOS
|
||||
<div id="first">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
<div id="second">
|
||||
</div>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.within "#second" do |scope|
|
||||
scope.should_see "Link"
|
||||
end
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
it "should fail if the string is not in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Link</a>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.should_see "Missing"
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
it "should fail if the regexp is not in the HTML" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page2">Different</a>
|
||||
EOS
|
||||
|
||||
lambda {
|
||||
@session.should_see /Li(n)[ck]/
|
||||
}.should raise_error
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user