NUnit Constraints

For some reason, I have ignored the NUnit syntax around Assert.That up to now. I thought it was one of these experiments around a fluent interface which didn’t offer any advantages over the existing API.

I wanted to write a test that tested whether a value was as expected, but within a certain range. I had previously used Assert.Equals with the overload that takes a delta.

Here is an alternative implementation:

[Test]
public void MinValue_IsCloseToMinimum_OfAllBids()
{
var lowPrice = new Price(10, 20);
var otherPrice = new Price(11, 20);

var priceViewModel =
new PriceViewModel(new List {lowPriceTolerance, otherPriceTolerance});

//old api
Assert.AreEqual(10, Convert.ToDouble(priceViewModel.MinValue), 0.2);

// new api
Assert.That(priceViewModel.MinValue, Is.EqualTo(10).Within(0.2));
}

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Uncategorized. Bookmark the permalink.