Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Display actual content in failure message #4

wants to merge 1 commit into from

3 participants

Tibor Claassen Jared Ning Wojtek Mach
Tibor Claassen

When doing BDD I find it very useful to see the actual content that failed to match along with the matcher itself. Did a basic implementation pretty much following the "Expected:" compared to "Actual:" pattern in the must_equal matcher.

How do you like it? Any suggestions for further improving it?

I was thinking of doing a pretty print for larger documents, but skipped it for the sake of simplicity.

Jared Ning

Actually, I'm leaning towards ending this gem in favor of @wojtekmach's minitest-capybara. It uses @zenspider's minitest-matcher, which is meant to bridge the gap between MiniTest and matchers such as Capybara's RSpec matchers. There's a lot of benefits to that including getting Capybara's default failure messages for free.

Any thoughts?

Tibor Claassen codebeige closed this June 06, 2012
Tibor Claassen

I am currently trying to set up some very lean Rails testing with MiniTest::Spec without all the magic known from Rspec. I want to find out, what I miss most and how all these things actually work under the hood. minitest-matcher is adding exactly that kind of complexity that I am currently trying to avoid.

When it comes to collaborative projects, me and my team will probably use something like 'minitest-capybara`at the end. So if you do not plan to keep this project alive, feel free to simply ignore my pull request.

Wojtek Mach

@codebeige maybe I should have pointed that out, but one of main reasons I created minitest-matchers is not because I like matchers. It was compatibility to existing libraries (valid_attribute was the first one), many are unfortunetly RSpec only. I even tried to make it a little more minitest-like and you can write must_have_content instead must have_content, there's a very subtle difference. Anyway, use the project and feel free to report any issues you encounter.

Tibor Claassen

@wojtekmach thank you for comment.

I like minitest-matcher and it is really good to have sort of a compatibility layer to existing libraries through it. I am also a big fan of Rspec and all the evolution it brought to testing/specing in general.

On the other hand I want to have a testing library that is as lean and fast as possible. I want to fully understand and control what is going on. For me MiniTestdoes a great job on that. I don't want to ruin that by bloating it unnecessarily.

Jared Ning

I was in the exact same place as you when I wrote this gem. I wanted to stay as far away from RSpec as possible. However, after looking through the internals of minitest and capybara, I don't feel using minitest-capybara is a complete "sell-out" for lack of better words. It's more of a use of duck-typing, if you will. And if you can see it like that, minitest-capybara is a more snug fit for accomplishing the same thing. You're not necessarily making anything more bloated.

If you want, you can just use minitest-matchers and bridge the gap between minitest and capybara yourself. It's really easy. Maybe 10 lines of code. There's even a mention of it in minitest-matcher's documentation. Plus you'll get more familiar with how things work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 04, 2012
Tibor Claassen Display actual content in failure message 1662555
This page is out of date. Refresh to see the latest.
15  lib/capybara_minitest_spec/matcher.rb
@@ -30,13 +30,18 @@ def test(page, *args)
30 30
31 31
     # Compose failure message.
32 32
     # E.g. Matcher failed: has_css?("expected", {:count => 1})
-    def self.failure_message(assertion_method, matcher_name, *args)
+    def self.failure_message(page, assertion_method, matcher_name, *args)
34 34
       if assertion_method == 'assert'
-        message = "Matcher failed: "
+        message = "Expected match: "
+        delimiter = "\n"
36 37
-        message = 'Matcher should have failed: '
+        message = "Expected no match: "
+        delimiter = "\n   "
38 40
-      message += "#{matcher_name}(#{', ')})"
+      message << "#{matcher_name}(#{', ')})"
+      message << delimiter 
+      message << "Actual content: "
+      message << page.to_s
40 45
41 46
42 47
@@ -54,7 +59,7 @@ def define_assertion
54 59
       matcher = self
55 60
       assertion_method = name.positive? ? 'assert' : 'refute'
56 61
       MiniTest::Assertions.send :define_method, name.assertion do |page, *args|
-        message = matcher.class.failure_message(assertion_method,, *args)
+        message = matcher.class.failure_message(page, assertion_method,, *args)
58 63
         send(assertion_method, matcher.test(page, *args), message)
59 64
60 65
9  test/matcher_spec.rb
@@ -33,13 +33,12 @@
33 33
34 34
35 35
   it "#failure_message with 'assert' arg returns positive assertion failure message" do
-    message = CapybaraMiniTestSpec::Matcher.failure_message('assert', :has_css?, {:option => 1})
-    message.must_equal 'Matcher failed: has_css?({:option=>1})'
+    message = CapybaraMiniTestSpec::Matcher.failure_message('<h1>Test</h1>', 'assert', :has_css?, {:option => 1})
+    message.must_equal "Expected match: has_css?({:option=>1})\nActual content: <h1>Test</h1>"
38 38
39 39
40 40
   it "#failure_message with 'refute' arg returns negative assertion failure message" do
-    message = CapybaraMiniTestSpec::Matcher.failure_message('refute', :has_css?, {:option => 1})
-    message.must_equal 'Matcher should have failed: has_css?({:option=>1})'
+    message = CapybaraMiniTestSpec::Matcher.failure_message('<h1>Test</h1>', 'refute', :has_css?, {:option => 1})
+    message.must_equal "Expected no match: has_css?({:option=>1})\n   Actual content: <h1>Test</h1>"
43 43
45 44

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.