Loading...
Loading...
Skill for writing fluent and readable test assertions with AwesomeAssertions. Use it when you need to write clear assertions, compare objects, validate collections, or handle complex comparisons. Covers complete APIs such as Should(), BeEquivalentTo(), Contain(), ThrowAsync(), etc. Keywords: assertions, awesome assertions, fluent assertions, Should(), Be(), BeEquivalentTo, Contain, ThrowAsync, NotBeNull, object comparison, collection validation, exception assertion, AwesomeAssertions, FluentAssertions, fluent syntax
npx skill4agent add kevintsengtw/dotnet-testing-agent-skills dotnet-testing-awesome-assertions-guide| Item | FluentAssertions | AwesomeAssertions |
|---|---|---|
| License | Paid for commercial projects | Apache 2.0 (completely free) |
| Namespace | | |
| API Compatibility | Original version | Highly compatible |
| Community Support | Officially maintained | Community maintained |
# .NET CLI
dotnet add package AwesomeAssertions
# Package Manager Console
Install-Package AwesomeAssertions<ItemGroup>
<PackageReference Include="AwesomeAssertions" Version="9.1.0" PrivateAssets="all" />
</ItemGroup>using AwesomeAssertions;
using Xunit;.Should()| Category | Common Methods | Description |
|---|---|---|
| Object | | Null value, type, and equality checks |
| String | | Content, pattern, and case-insensitive comparison |
| Number | | Comparison, range, and floating-point precision |
| Collection | | Quantity, content, order, and condition checks |
| Exception | | Exception type, message, and nested exception checks |
| Async | | Async exception and completion validation |
📖 For complete syntax examples and code, refer to references/core-assertions-syntax.md
BeEquivalentTo()optionsoptions.Excluding(u => u.Id)options.Excluding(ctx => ctx.Path.EndsWith("At"))options.IgnoringCyclicReferences().WithMaxRecursionDepth(10)product.Should().BeValidProduct()ExcludingAuditFields()📖 For complete examples, refer to references/complex-object-assertions.md
HaveCount()BeEquivalentToExcludingMissingMembers()// Selective property comparison — only validate key fields
order.Should().BeEquivalentTo(new
{
CustomerId = 123,
TotalAmount = 999.99m,
Status = "Pending"
}, options => options.ExcludingMissingMembers());Method_Scenario_ExpectedResultCreateUser_ValidEmail_ShouldReturnEnabledUserbecauseresult.IsSuccess.Should().BeFalse("because negative payment amounts are not allowed");AssertionScopeusing (new AssertionScope())
{
user.Should().NotBeNull("User creation should not fail");
user.Id.Should().BeGreaterThan(0, "User should have valid ID");
user.Email.Should().NotBeNullOrEmpty("Email is required");
}| Scenario | Key Techniques |
|---|---|
| API Response Validation | |
| Database Entity Validation | |
| Event Validation | Capture events via subscription and validate properties one by one |
📖 For complete code examples, refer to references/common-scenarios.md
// Exclude dynamic fields
actual.Should().BeEquivalentTo(expected, options => options
.Excluding(x => x.Id)
.Excluding(x => x.CreatedAt)
.Excluding(x => x.UpdatedAt)
);// Use BeEquivalentTo to ignore order
actual.Should().BeEquivalentTo(expected); // Does not check order
// Or explicitly specify that order needs to be checked
actual.Should().Equal(expected); // Checks order// Use precision tolerance
actualValue.Should().BeApproximately(expectedValue, 0.001);unit-test-fundamentals[Fact]
public void Calculator_Add_TwoPositiveNumbers_ShouldReturnSum()
{
// Arrange - Follow 3A Pattern
var calculator = new Calculator();
// Act
var result = calculator.Add(2, 3);
// Assert - Use AwesomeAssertions
result.Should().Be(5);
}test-naming-conventions[Fact]
public void CreateUser_ValidData_ShouldReturnEnabledUser()
{
var user = userService.CreateUser("test@example.com");
user.Should().NotBeNull()
.And.BeOfType<User>();
user.IsActive.Should().BeTrue();
}xunit-project-setup