views:

346

answers:

3

I've inherited some unit test code which is giving me a deprecation warning because it uses "Assertion.AssertEquals":

warning CS0618: 'NUnit.Framework.Assertion' is obsolete: 'Use Assert class instead'

However, I can't see the obvious method in the Assert class that I should be using instead?

AssertEquals takes two objects and a message that can be used to report the error if there's a failure. e.g.

        Assertion.AssertEquals(
             "Enqueuing first item should set count to 1",
             1, pq.Count);

What's the equivalent method on the Assert class?

+4  A: 
Assert.That(a, Is.EqualTo(b),
    "Enqueuing first item should set count to 1");
John Gietzen
Yeah but that ignores the message!
Paul Hollingsworth
`Assert.That(a, Is.EqualTo(b), "FAILURE")`
The Chairman
+4  A: 

How about this:

Assert.AreEqual(1, pq.Count,
                "Enqueuing first item should set count to 1");
Jon Skeet
+7  A: 

The answer Jon Skeet presented points to the so called "Classic" model whereas John Gietzen's answer refers to the "Constraint-based" model. Both are right and both do provide the possibility to pass a message for the case of failure.

So let me conclude this:

"Classic" model

Assert.AreEqual(1, pq.Count,
    "Enqueuing first item should set count to 1");

"Constraint-based" model

Assert.That(
    pq.Count,
    Is.EqualTo(1),
    "Enqueuing first item should set count to 1");

I prefer the latter one since it reads more like a sentence.

The Chairman
True... if you combined your comments into a single answer I would mark that as accepted
Paul Hollingsworth