Skip to content

Commit

Permalink
Migrate to AwesomeAssertions and update config
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwaldron committed Feb 26, 2025
1 parent e9a0d10 commit c59b84a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="AwesomeAssertions" Version="8.0.1" />
<PackageVersion Include="AwesomeAssertions.Analyzers" Version="0.34.2" />
<PackageVersion Include="ClosedXML" Version="0.104.2" />
<PackageVersion Include="CsvHelper" Version="33.0.1" />
<PackageVersion Include="Dapper" Version="2.1.35" />
Expand Down Expand Up @@ -32,7 +34,6 @@
<PackageVersion Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="EfCore.TestSupport" Version="9.0.0" />
<PackageVersion Include="FluentAssertions" Version="[7.0.0,8.0)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
Expand Down
26 changes: 4 additions & 22 deletions tests/AppServicesTests/AppServicesTestsSetup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using AutoMapper;
using FluentAssertions.Equivalency;
using FluentAssertions.Extensions;
using Microsoft.AspNetCore.Identity;
using Cts.AppServices.AutoMapper;
using Cts.Domain.Identity;
using FluentAssertions.Extensions;

namespace AppServicesTests;

Expand All @@ -19,30 +16,15 @@ public void OneTimeSetUp()
Mapper = new MapperConfiguration(configuration => configuration.AddProfile(new AutoMapperProfile()))
.CreateMapper();

AssertionOptions.AssertEquivalencyUsing(options => options
AssertionConfiguration.Current.Equivalency.Modify(options => options
// Setting this option globally since our DTOs generally exclude properties, e.g., audit properties.
// See: https://fluentassertions.com/objectgraphs/#matching-members
.ExcludingMissingMembers()

// DateTimeOffset comparison is often off by a few microseconds.
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 10.Milliseconds()))
.Using<DateTimeOffset>(
context => context.Subject.Should().BeCloseTo(context.Expectation, 10.Milliseconds()))
.WhenTypeIs<DateTimeOffset>()

// Exclude some concurrency properties automatically added by ASP.NET Identity.
// See: https://stackoverflow.com/a/57406982/212978
.Using(new IdentityUserSelectionRule())
);
}

private class IdentityUserSelectionRule : IMemberSelectionRule
{
public IEnumerable<IMember> SelectMembers(INode currentNode, IEnumerable<IMember> selectedMembers,
MemberSelectionContext context) =>
selectedMembers.Where(e =>
!(e.DeclaringType.Name.StartsWith(nameof(IdentityUser)) &&
e.Name is nameof(ApplicationUser.SecurityStamp) or nameof(ApplicationUser.ConcurrencyStamp)));

public bool IncludesMembers => false;
public override string ToString() => "Exclude SecurityStamp and ConcurrencyStamp from IdentityUser";
}
}
6 changes: 5 additions & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions"/>
<PackageReference Include="AwesomeAssertions" />
<PackageReference Include="AwesomeAssertions.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="NSubstitute"/>
<PackageReference Include="NSubstitute.Analyzers.CSharp">
Expand Down
5 changes: 3 additions & 2 deletions tests/EfRepositoryTests/EfRepositoryTestsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public class EfRepositoryTestsSetup
[OneTimeSetUp]
public void RunBeforeAllTests()
{
AssertionOptions.AssertEquivalencyUsing(opts => opts
AssertionConfiguration.Current.Equivalency.Modify(options => options
// DateTimeOffset comparison is often off by a few microseconds.
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 10.Milliseconds()))
.Using<DateTimeOffset>(
context => context.Subject.Should().BeCloseTo(context.Expectation, 10.Milliseconds()))
.WhenTypeIs<DateTimeOffset>()
);
}
Expand Down
30 changes: 0 additions & 30 deletions tests/LocalRepositoryTests/LocalRepositoryTestsSetup.cs

This file was deleted.

0 comments on commit c59b84a

Please sign in to comment.