Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Display actual content in failure message #4

Closed
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
Owner

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
Owner

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})
33  
-    def self.failure_message(assertion_method, matcher_name, *args)
  33
+    def self.failure_message(page, assertion_method, matcher_name, *args)
34 34
       if assertion_method == 'assert'
35  
-        message = "Matcher failed: "
  35
+        message = "Expected match: "
  36
+        delimiter = "\n"
36 37
       else
37  
-        message = 'Matcher should have failed: '
  38
+        message = "Expected no match: "
  39
+        delimiter = "\n   "
38 40
       end
39  
-      message += "#{matcher_name}(#{args.map(&:inspect).join(', ')})"
  41
+      message << "#{matcher_name}(#{args.map(&:inspect).join(', ')})"
  42
+      message << delimiter 
  43
+      message << "Actual content: "
  44
+      message << page.to_s
40 45
     end
41 46
 
42 47
     private
@@ -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|
57  
-        message = matcher.class.failure_message(assertion_method, matcher.name.original, *args)
  62
+        message = matcher.class.failure_message(page, assertion_method, matcher.name.original, *args)
58 63
         send(assertion_method, matcher.test(page, *args), message)
59 64
       end
60 65
     end
9  test/matcher_spec.rb
@@ -33,13 +33,12 @@
33 33
   end
34 34
 
35 35
   it "#failure_message with 'assert' arg returns positive assertion failure message" do
36  
-    message = CapybaraMiniTestSpec::Matcher.failure_message('assert', :has_css?, {:option => 1})
37  
-    message.must_equal 'Matcher failed: has_css?({:option=>1})'
  36
+    message = CapybaraMiniTestSpec::Matcher.failure_message('<h1>Test</h1>', 'assert', :has_css?, {:option => 1})
  37
+    message.must_equal "Expected match: has_css?({:option=>1})\nActual content: <h1>Test</h1>"
38 38
   end
39 39
 
40 40
   it "#failure_message with 'refute' arg returns negative assertion failure message" do
41  
-    message = CapybaraMiniTestSpec::Matcher.failure_message('refute', :has_css?, {:option => 1})
42  
-    message.must_equal 'Matcher should have failed: has_css?({:option=>1})'
  41
+    message = CapybaraMiniTestSpec::Matcher.failure_message('<h1>Test</h1>', 'refute', :has_css?, {:option => 1})
  42
+    message.must_equal "Expected no match: has_css?({:option=>1})\n   Actual content: <h1>Test</h1>"
43 43
   end
44  
-
45 44
 end
Commit_comment_tip

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.