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]Equal | Can add custom comparer |
Are[Not]Same | |
IsTrue, IsFalse | |
GreaterThan, LessThan GreaterThanOrEqualTo, LessThanOrEqualTo | Can add custom comparer |
Is[Not]Null | |
[Not]Like | Tests string contains full or partial match of RegEx |
FullMatch | Tests for exact match with RegEx |
StartWith, EndsWith | Case Sensitive comparison of strings |
[Not]Between | Compares actual with Min/Max, Can add custom comparer |
Are[Not]ApproximatelyEqual | Tests for equality with a delta |
Comparison Operators – Sets
Is[Not]Empty | Tests for Empty/NotEmpty IEnumerable |
Contains, DoesNotContain | Tests for element in IEnumerable Tests for substring in string |
ContainsKey, DoesNotContainKey | Tests for existence of Key in a Dictionary |
AreElements[Not]Equal | Tests elements for equality (order dependent) Can add a custom comparer |
AreElementsEqualIgnoringOrder | Tests 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]InstanceOfType | Tests 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!
5c204462-bcb3-45be-8a65-3d075c3fbeca|0|.0|27604f05-86ad-47ef-9e05-950bb762570c