What is the difference between Classic and Constraint Model Assertions in Nunit?

2k views Asked by At

I am learning Nunit-2.6.3 by reading the Documentation. I am having a few doubts about it.

What is the difference between the classical model and the constraint model assertion?

Which model of assertions is the best one, and why?

1

There are 1 answers

0
forsvarir On BEST ANSWER

The main difference is syntactic. It's the difference between (classic):

Assert.AreEqual("expected", someString);

And (constraint)

Assert.That(someString, Is.EqualTo("expected"));

Classic mode has been around longer and some people believe that it's more explicit and easier to follow.

Other people believe the constraint based approach is closer to the way that you might say the constraint if you were explaining it to somebody else.

If you're just getting started, then probably the constraint based assertions are the better ones to learn, since they're the direction that NUnit appears to be trying to head in. They're also closer to FluentAssertions. The constraint based assertions also has more explicit support for extension through the use of the IResolveConstraint interface.

You should however probably gain an awareness of the classic assertions since there's a good chance that different places you encounter code may use either depending on what they used first.

Although the syntax is different, what they're doing is very similar, so if you understand one set of assertions, converting them back and forth is pretty straightforward.