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
