Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have found custom failure message helpful in rather limited number of cases. Particulary, when you assert several properties of objects which <em>expected/actual</em> values might not give the best information about what (and why) failed. Consider a test such as:</p> <pre><code>[Test] public void CreateOrder_CreatesValidOrderForProvidedCustomer() { // Assume we arranged test here var order = orderFactory.Create(customer); Assert.That(order.Type, Is.EqualTo("Immediate Dispatch")); Assert.That(order.Details, Is.EqualTo("Very Important Package")); Assert.That(order.CustomerNote, Is.EqualTo("Send fast or I tell mom")); } </code></pre> <p>When any of the asserts above fails, message you'll get will be something along the lines:</p> <blockquote> <p><em>Expected: "Very Important Package" Actual: "Dispatch quickly"</em>.</p> </blockquote> <p>It's hard to tell <em>which</em> property caused that without looking at unit test (data it uses to be precise). Simply adding <strong>property name</strong> as a failure message helps here.</p> <p>But!</p> <p>This was just extreme case, when multiple asserted properties might be hard to differentiate <em>content-wise</em>. Usually, you shouldn't be facing such problems (note that we solved it with providing <strong><em>one</strong> word long</em> expection message). If you feel the need to use long and descriptive expection message, it might be a sign that your test is overly complicated. You might consider splitting it into few tests, or maybe even redesigning tested class.</p> <p>On top of that, I suggest having a look at projects like <a href="http://fluentassertions.codeplex.com/" rel="nofollow">FluentAssertions</a>. They got it right:</p> <blockquote> <p>Nothing is more annoying then a unit test that fails without clearly explaining why.</p> </blockquote> <p>And solved the issue with very clean syntax and neat error reporting:</p> <pre><code>"1234567890".Should().Be("0987654321"); </code></pre> <p>Will be reported as: </p> <blockquote> <p>Expected string to be<br> "0987654321", but<br> "1234567890" differs near "123" (index 0). </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload