From 1a4db57d93d0f18350b81e61115677af2c510808 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 14 Sep 2008 14:54:29 -0400 Subject: [PATCH] Add should_see and should_not_see assertions --- Rakefile | 2 +- lib/webrat/core/assertions.rb | 27 ++++++++++++ lib/webrat/core/scope.rb | 1 + lib/webrat/core/session.rb | 2 + spec/api/should_not_see_spec.rb | 73 +++++++++++++++++++++++++++++++++ spec/api/should_see_spec.rb | 73 +++++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 lib/webrat/core/assertions.rb create mode 100644 spec/api/should_not_see_spec.rb create mode 100644 spec/api/should_see_spec.rb diff --git a/Rakefile b/Rakefile index 601fac3..9bd083a 100644 --- a/Rakefile +++ b/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" diff --git a/lib/webrat/core/assertions.rb b/lib/webrat/core/assertions.rb new file mode 100644 index 0000000..7161280 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 42df3d7..559204f 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -4,6 +4,7 @@ module Webrat class Scope include Logging include Flunk + include Assertions def initialize(session, html, selector = nil) @session = session diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 7e9d9c6..6142877 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -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 \ No newline at end of file diff --git a/spec/api/should_not_see_spec.rb b/spec/api/should_not_see_spec.rb new file mode 100644 index 0000000..6e58b23 --- /dev/null +++ b/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 + Link + 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 + Link + 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 +
+ Link +
+
+
+ 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 +
+ Link +
+
+
+ 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 + Link + EOS + + @session.should_not_see "Missing" + end + + it "should pass if the regexp is not in the HTML" do + @session.response_body = <<-EOS + Different + EOS + + @session.should_not_see /Li(n)[ck]/ + end +end diff --git a/spec/api/should_see_spec.rb b/spec/api/should_see_spec.rb new file mode 100644 index 0000000..9ae021f --- /dev/null +++ b/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 + Link + EOS + + @session.should_see "Link" + end + + it "should pass if the regexp is in the HTML" do + @session.response_body = <<-EOS + Link + EOS + + @session.should_see /Li(n)[ck]/ + end + + it "should pass if the string is in the HTML scope" do + @session.response_body = <<-EOS +
+ Link +
+
+
+ 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 +
+ Link +
+
+
+ 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 + Link + 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 + Different + EOS + + lambda { + @session.should_see /Li(n)[ck]/ + }.should raise_error + end +end