Skip to content

Commit

Permalink
Add should_see and should_not_see assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Oct 14, 2008
1 parent 44db244 commit 1a4db57
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions lib/webrat/core/assertions.rb
@@ -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
1 change: 1 addition & 0 deletions lib/webrat/core/scope.rb
Expand Up @@ -4,6 +4,7 @@ module Webrat
class Scope
include Logging
include Flunk
include Assertions

def initialize(session, html, selector = nil)
@session = session
Expand Down
2 changes: 2 additions & 0 deletions lib/webrat/core/session.rb
Expand Up @@ -141,5 +141,7 @@ def rewrite_css_and_image_references(response_html) # :nodoc
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 changes: 73 additions & 0 deletions spec/api/should_not_see_spec.rb
@@ -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 changes: 73 additions & 0 deletions spec/api/should_see_spec.rb
@@ -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

0 comments on commit 1a4db57

Please sign in to comment.