liveBook · Manning

Assert.Equal

As explained in Pro ASP.NET Core 7, Tenth Edition

Assert.Equal is an xUnit.net assertion that verifies that two values are equal, optionally using an equality comparer.

Assert.Equal verifies that two values are equal in an xUnit.net test. For a changed property, compare the expected value with the value currently held by the object; using the original value tests the wrong behavior. section 6.3.4

Checking changed product values

After assigning p.Name = "New Name" and p.Price = 200M, use Assert.Equal("New Name", p.Name) and Assert.Equal(200M, p.Price). Rerunning the tests confirms the corrected assertions pass. section 6.3.4

For custom objects, pass an IEqualityComparer<T> to define which properties constitute equality. A reusable comparer can accept a lambda, allowing collection assertions to compare products by name and price. section 6.3.5

Listing 6.12 Correcting a test in the ProductTests.cs file in the SimpleApp.Tests folder
using SimpleApp.Models;
using Xunit;

namespace SimpleApp.Tests {

    public class ProductTests {
        
        [Fact]
        public void CanChangeProductName() {
                
            // Arrange
            var p = new Product { Name = "Test", Price = 100M };
                        
            // Act
            p.Name = "New Name";
                        
            //Assert
            Assert.Equal("New Name", p.Name);
        }
                
        [Fact]
        public void CanChangeProductPrice() {
                
            // Arrange
            var p = new Product { Name = "Test", Price = 100M };
                        
            // Act
            p.Price = 200M;
                        
            //Assert
                        
            Assert.Equal(200M, p.Price);
        }
    }
}

Use the complete ProductTests listing to see Assert.Equal applied to mutable model properties inside Arrange, Act, and Assert tests. section 6.3.4

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains direct assertions and comparer-based object equality, but stops short of broader xUnit.net assertion behavior.
Examples
Uses changed Product properties and controller-provided Product collections as assertion targets.
What it assumes
Assumes basic C# objects and unit-test structure while demonstrating equality comparison from direct properties through custom comparers.
What you can do after
Diagnose/debug unit tests by checking expected values and defining custom equality comparisons.
How it connects
Connects equality assertions with unit testing, IEqualityComparer<T>, model classes, controllers, and view models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.