|
| 1 | +# Asserter |
| 2 | + |
| 3 | +As explained in the [Overview](Overview.md), the Asserter is a collection of additional and improved code assertion methods where the MSTest Assert equivalent doesn't provide useful messages when the assertion fails. |
| 4 | + |
| 5 | +The Asserter provides the following code assertion extensions: |
| 6 | + |
| 7 | +## IsTrue |
| 8 | + |
| 9 | +This method tests whether the specified condition is true and throws an exception if the condition is false. |
| 10 | + |
| 11 | +The method comes as an improvement over the MSTest `Assert.IsTrue` for two reasons. Firstly, the failure message provides useful information showing what is false. More importantly, the condition parameter takes an Expression which allows you to build up an assertion through multiple parameters. |
| 12 | + |
| 13 | +**Example** |
| 14 | + |
| 15 | +```csharp |
| 16 | +[TestMethod] |
| 17 | +public void TestObject_WhenBooleanMethodCalledWithTrueParameter_ShouldReturnTrueAndSetBooleanPropertyTestToFalse() |
| 18 | +{ |
| 19 | + // Arrange |
| 20 | +
|
| 21 | + var o = new TestObject { BooleanPropertyTest = true }; |
| 22 | + |
| 23 | + // Act & Assert |
| 24 | +
|
| 25 | + Asserter.IsTrue(() => !o.BooleanPropertyTest && o.BooleanMethodTest(true)); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## IsFalse |
| 30 | + |
| 31 | +This method tests whether the specified condition is false and throws an exception if the condition is true. |
| 32 | + |
| 33 | +The method comes as an improvement over the MSTest `Assert.IsFalse` for two reasons. Firstly, the failure message provides useful information showing what is true. More importantly, the condition parameter takes an Expression which allows you to build up an assertion through multiple parameters. |
| 34 | + |
| 35 | +**Example** |
| 36 | + |
| 37 | +```csharp |
| 38 | +[TestMethod] |
| 39 | +public void TestObject_WhenBooleanMethodCalledWithFalseParameter_ShouldSetBooleanPropertyTestToFalse() |
| 40 | +{ |
| 41 | + // Arrange |
| 42 | +
|
| 43 | + var o = new TestObject { BooleanPropertyTest = true }; |
| 44 | + |
| 45 | + // Act |
| 46 | +
|
| 47 | + o.BooleanMethodTest(false); |
| 48 | + |
| 49 | + // Assert |
| 50 | +
|
| 51 | + Asserter.IsFalse(() => o.BooleanPropertyTest); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## IsNull |
| 56 | + |
| 57 | +This method tests whether the specified condition is null and throws an exception if the condition is not null. |
| 58 | + |
| 59 | +The method comes as an improvement over the MSTest `Assert.IsNull` for two reasons. Firstly, the failure message provides useful information showing what is not null. More importantly, the condition parameter takes an Expression which allows you to build up an assertion through multiple parameters. |
| 60 | + |
| 61 | +**Example** |
| 62 | + |
| 63 | +```csharp |
| 64 | +[TestMethod] |
| 65 | +public void TestObject_WhenNullableBooleanMethodCalledWithNullParameter_ShouldSetNullableBooleanPropertyTestToNull() |
| 66 | +{ |
| 67 | + // Arrange |
| 68 | +
|
| 69 | + var o = new TestObject { NullableBooleanPropertyTest = true }; |
| 70 | + |
| 71 | + // Act |
| 72 | +
|
| 73 | + o.NullableBooleanMethodTest(null); |
| 74 | + |
| 75 | + // Assert |
| 76 | +
|
| 77 | + Asserter.IsNull(() => o.NullableBooleanPropertyTest); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## IsNotNull |
| 82 | + |
| 83 | +This method tests whether the specified condition is not null and throws an exception if the condition is null. |
| 84 | + |
| 85 | +The method comes as an improvement over the MSTest `Assert.IsNotNull` for two reasons. Firstly, the failure message provides useful information showing what is null. More importantly, the condition parameter takes an Expression which allows you to build up an assertion through multiple parameters. |
| 86 | + |
| 87 | +**Example** |
| 88 | + |
| 89 | +```csharp |
| 90 | +[TestMethod] |
| 91 | +public void TestObject_WhenNullableBooleanMethodCalledWithTrueParameter_ShouldSetNullableBooleanPropertyTestToTrue() |
| 92 | +{ |
| 93 | + // Arrange |
| 94 | +
|
| 95 | + var o = new TestObject { NullableBooleanPropertyTest = null }; |
| 96 | + |
| 97 | + // Act |
| 98 | +
|
| 99 | + o.NullableBooleanMethodTest(true); |
| 100 | + |
| 101 | + // Assert |
| 102 | +
|
| 103 | + Asserter.IsNotNull(() => o.NullableBooleanPropertyTest); |
| 104 | +} |
| 105 | +``` |
0 commit comments