liveBook · Manning

Assertion

As explained in ASP.NET Core in Action, 3e

An assertion is a test operation that verifies an actual result satisfies an expected condition.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains value and exception assertions with runnable examples, but defers broader ASP.NET Core testing.
Examples
Demonstrates currency conversion, mismatched expected values, multiple InlineData cases, and zero-rate exceptions.
What it assumes
Assumes basic C# and test-method structure while introducing assertions as a testing concept.
What you can do after
Apply it via xUnit to verify returned values, repeated input cases, and exceptions raised by a method.
How it connects
Ties assertions to xUnit, unit testing, Arrange-Act-Assert, and parameterized testing.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.