Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 18 | Comments: 79

filter by tags archive

Got to debug is a bug, fix your error messages

time to read 1 min | 144 words

This is part of the RavenDB test suite.

image

It has a bug.

Can you see it?

One of the things that I learned when working on the Castle project is that errors are important. In fact, they are really important, so it is worth the time to check them very carefully.

In this case, this code would fail but won’t tell me why it is failing.

Changing it to this:

image

Means that I get the actual error message right there and then, no need to do anything special.


Comments

Scooletz

Any with predicate will return true if the backupStatus.Messages is empty, so the Assert.False will throw. One of the solutions is what you provided, the second doing .Any() && .Any(yourPredicate)

Stefan

What about all other messages with severity error other than the first? They still get dropped.

Why not use Assert.Fail(message.Message); ? I find Assert.False(true,...) confusing.

Pop Catalin

This is a bit of funny code: if (message != null) Assert.False(true, message.Message);

expecially the 'Assert.False(true...' part.

Can easily be replaced with:

Assert.Fail(message.Message) or Assert.IsNotNull(message, message.Message)

Pop Catalin

Correction for above: Assert.IsNull should be used

Ryan

Assert.False(true,..) is a really confusing way to write an assertion.

Josh Kodroff

Umm... what's wrong with:

Assert.That(() => backup.Messages.Any(x => x.Severity == BackupStatus.BackupMessageSeverity.Error), Is.True, "Expected to find a backup message error, but didn't find one.");

Pretty explicit in my mind.

João P. Bragança

He needs to show the actual error message in the tests.

Karep

I hate Assert.False(true.... I was wondering if that was some typo.

Karep

And also why you once break out of while (and method) and in other place you return?

Matthew Wills

I'd likely lean towards:

var message = backupStatus.Messages.Where(x => x.Severity == BackupStatus.BackupMessageSeverity.Error).Select(z => z.Message).FirstOrDefault();

Assert.IsNull(message);

to avoid the if.

Matthew Wills

Whoops, IsNull(message) should be IsNull(message, message).

Gian Maria

When a unit test fails the name of the unit test is important, but if you have some more descriptive failure message it will help you finding the real cause of the test failure.

I agree with matthew that an Assert.IsNull is a clearer form of assertion, but the key point is including the real error message in the unit test result to have a quick hint of why the test is failing.

Lookman

It will throw an exception if x.Severity is null.

Matthew Wills

Gian,

I agree with matthew that an Assert.IsNull is a clearer form of assertion, but the key point is including the real error message in the unit test result to have a quick hint of why the test is failing.

Assert.IsNull(message, message) would do this (the second parameter being the failed assertion message).

It will throw an exception if x.Severity is null.

I don't think any of the proposed solutions will do that. They may have issues if backupStatus.Messages was null, or contained a null entry, though.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. The insidious cost of allocations - one day from now
  2. Buffer allocation strategies: A possible solution - 4 days from now
  3. Buffer allocation strategies: Explaining the solution - 5 days from now
  4. Buffer allocation strategies: Bad usage patterns - 6 days from now
  5. The useless text book algorithms - 7 days from now

And 1 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    03 Sep 2015 - The industry at large
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats