Permalink
Browse files

Make integration spec failures more descriptive

  • Loading branch information...
1 parent 970f3a3 commit 21418f18905b4c8cc481383aa9a418b8829df173 @haines haines committed Nov 16, 2012
Showing with 50 additions and 5 deletions.
  1. +6 −5 spec/integration/integration_spec.rb
  2. +44 −0 spec/support/matchers/have_text.rb
@@ -1,22 +1,23 @@
require 'spec_helper'
require 'support/dummy_app'
+require 'support/matchers/have_text'
shared_examples_for "a decorator in a view" do
it "works" do
# it runs in the correct environment
- page.should have_css "#environment", text: environment
+ page.should have_text(environment).in("#environment")
# it can use path helpers with a model
- page.should have_css "#path_with_model", text: "/en/posts/1"
+ page.should have_text("/en/posts/1").in("#path_with_model")
# it can use path helpers with an id
- page.should have_css "#path_with_id", text: "/en/posts/1"
+ page.should have_text("/en/posts/1").in("#path_with_id")
# it can use url helpers with a model
- page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1"
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_model")
# it can use url helpers with an id
- page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1"
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_id")
end
end
@@ -0,0 +1,44 @@
+module HaveTextMatcher
+ def have_text(text)
+ HaveText.new(text)
+ end
+
+ class HaveText
+ def initialize(text)
+ @text = text
+ end
+
+ def in(css)
+ @css = css
+ self
+ end
+
+ def matches?(subject)
+ @subject = subject
+
+ @subject.has_css?(@css || "*", text: @text)
+ end
+
+ def failure_message_for_should
+ "expected to find #{@text.inspect} #{within}"
+ end
+
+ def failure_message_for_should_not
+ "expected not to find #{@text.inspect} #{within}"
+ end
+
+ private
+
+ def within
+ "#{inside} within\n#{@subject.html}"
+ end
+
+ def inside
+ @css ? "inside #{@css.inspect}" : "anywhere"
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.include HaveTextMatcher
+end

0 comments on commit 21418f1

Please sign in to comment.