An assertion verifies that an actual result satisfies an expected condition. In an xUnit test, it checks the outcome of the Act step and fails the test when the expectation is not met. section 35.5
Use assertions to check:
- Returned values, such as confirming that currency conversion produces the expected amount with Assert.Equal(). section 35.5
- Failure behavior, such as confirming that an invalid exchange rate raises an ArgumentException with Assert.Throws(). section 35.6
- Several input combinations by placing the same assertion in a parameterized [Theory] test. section 35.5
Checking a converted value
Arrange a CurrencyConverter with value 3, rate 1.5, and four decimal places. Act by calling ConvertToGbp(). Assert with Assert.Equal(expected, actual), where expected is 2. If the values differ, xUnit fails the test and reports the mismatch. section 35.5
Listing 35.4 Unit test for ConvertToGbp using expected arguments
[Fact] #1 public void ConvertToGbp_ConvertsCorrectly() #2 { var converter = new CurrencyConverter(); #3 decimal value = 3; #4 decimal rate = 1.5m; #4 int dp = 4; #4 decimal expected = 2; #5 var actual = converter.ConvertToGbp(value, rate, dp); #6 Assert.Equal(expected, actual); #7 } #1 The [Fact] attribute marks the method as a test method. #2 You can call the test anything you like. #3 The class to test, commonly called the “system under test” #4 The parameters of the test that will be passed to ConvertToGbp #5 The result you expect #6 Executes the method and captures the result #7 Verifies that the expected and actual values match; if they don’t, throws an exception
Inspect the complete test layout, including the test attribute, system under test, input values, expected result, method call, and assertion. section 35.5














