All posts tagged 'TDD'

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer

NAVIGATION - SEARCH

My Favorite MbUnit Assertions

This post is the second of two parts (the first on MbUnit Attributes is here).  There are many new, renamed, improved, combined, etc attributes in MbUnit v3.  Jeff Brown et al have done a fantastic job with the newest versions of Gallio and MbUnit (download here).  (These are not in any particular order.)

Assertions

Positive and Negative Tests

A nice, consistent update in version 3 is just about every Assertion has a correlated negative Assertion.  For example, for AreEqual there is also AreNotEqual.  Most of the names are obvious like this, except for a few logical differences (Assert.IsTrue and Assert.IsFalse).

Exceptions

I mentioned this transition from Attribute based exception handling to Assertion base in my last post, but I think it is worth repeating.  This allows for more granular control of the test than simply decorating it with the ExceptedException attribute.

There are two flavors, Assert.Throws and Assert.DoesNotThrow.  (I’ve never seen a use for the latter, but it’s there).

[Test]
public void Should_Test_For_Exception_Thrown()
{
   var ex = Assert.Throws<ArgumentNullException>(
       () => { throw new ArgumentNullException(exMessage); });
}

Comparison Operators – Single Values


There is a full suite of comparer assertions (see table below), and they essentially work the same way (see IntelliSense for more detailed information):



Are[Not]EqualCan add custom comparer
Are[Not]Same 
IsTrue, IsFalse 
GreaterThan, LessThan
GreaterThanOrEqualTo, LessThanOrEqualTo
Can add custom comparer
Is[Not]Null 
[Not]LikeTests string contains full or partial match of RegEx
FullMatchTests for exact match with RegEx
StartWith, EndsWithCase Sensitive comparison of strings
[Not]BetweenCompares actual with Min/Max,
Can add custom comparer
Are[Not]ApproximatelyEqualTests for equality with a delta

Comparison Operators – Sets



Is[Not]EmptyTests for Empty/NotEmpty IEnumerable
Contains, DoesNotContainTests for element in IEnumerable
Tests for substring in string
ContainsKey, DoesNotContainKeyTests for existence of Key in a Dictionary
AreElements[Not]EqualTests elements for equality (order dependent)
Can add a custom comparer
AreElementsEqualIgnoringOrderTests elements for equality (order agnostic)
Can add a custom comparer

Specialty Operators




Is[Not]AssignableFrom

Tests that object can be assigned a variable of expectedType
Is[Not]InstanceOfTypeTests that an object is an instance of the expected type
Note: inheritance chains will cause a failure

So, there are the Assertions for MbUnit v3. Great stuff from the Gallio/MbUnit team.


Happy Coding!

Managed Windows Shared Hosting by OrcsWeb