Tony DiNitto
Tony DiNitto

Reputation: 1239

Why does Minitest seem to not always report what the 'Actual' results of the test are?

Why does MiniTest sometimes truly display what's 'Actual' and sometimes it will just show a nil/blank/quote.

Example - I know that the value of a.join has "Hi" displayed on 2 lines with a blank line.

class Question

  def stack
    "Hi\n" \
    "Hi\n"  
  end

  def overflow
    a = []
    a << stack + "\n"   # This + "\n" is in question
    a.join              # This is "hi"s
  end
end

a = Question.new
puts a.overflow

If I take out the + "\n" the test will say the "Actual" is just a quote instead of what it actually is (which is "Hi" printed on 2 lines). If I replace 'a.join' with "Tadah!" then Minitest will then show "Tadah!" under the "Actual".

I have the following test:

def test_verses
  expected = "Hi\n" \
  "Hi\n\n"
  assert_equal expected, Question.new.overflow
end

I've seen this happen several times. I can never tell if I TRULY have a nil result, or if Minitest is just "lying" to me again for some reason.

Thx as always!

Upvotes: 0

Views: 380

Answers (1)

matt
matt

Reputation: 79733

The output from Minitest is a diff of the expected result and the actual (I think it actually calls the external diff command). There isn’t separate ‘expected’ and ‘actual’ sections, it’s all mixed together in a format that shows the differences.

In this case, when you remove the `"\n", the failure looks something like this:

  1) Failure:
Foo#test_verses [foo.rb:22]:
--- expected
+++ actual
@@ -1,4 +1,3 @@
 "Hi
 Hi
-
 "

The --- expected and +++ actual indicate that in the diff (which is shown after the @@... line) lines that start with - were expected to appear but didn’t, and lines that start with + appeared in the result but shouldn’t have. In this example there is an empty line that starts with -. This means that a newline was expected to appear in the output, but didn’t. This corresponds to the + "\n" that you removed, but the test was expecting. The quote on the line following the - line is the end quote for the entire diff, not the contents of the ‘expected’ section.

When you change the return value of the overflow method to "Tadah!", the failure message looks like this:

  1) Failure:
Foo#test_verses [foo.rb:23]:
--- expected
+++ actual
@@ -1,4 +1 @@
-"Hi
-Hi
-
-"
+"Tadah!"

Looking at the lines starting with - you can see the entire expected message was missing, and the line starting with + shows there was an extra line containing Tadah!. This case is also different in that Minitest has displayed the output as two separate strings (note the two separate sets of quotes).

Upvotes: 2

Related Questions