Skip to content

Commit

Permalink
feat: Replace FluentAssertions with Shouldy
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 committed Jan 16, 2025
1 parent cd4c362 commit d31fc62
Show file tree
Hide file tree
Showing 23 changed files with 189 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MockQueryable.NSubstitute" Version="7.0.3" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using CleanArchitecture.Application.Queries.Tenants.GetAll;
using CleanArchitecture.Application.Tests.Fixtures.Queries.Tenants;
using CleanArchitecture.Application.ViewModels;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Application.Tests.Queries.Tenants;
Expand All @@ -29,11 +29,15 @@ public async Task Should_Get_Existing_Tenant()

_fixture.VerifyNoDomainNotification();

result.PageSize.Should().Be(query.PageSize);
result.Page.Should().Be(query.Page);
result.Count.Should().Be(1);
result.PageSize.ShouldBe(query.PageSize);
result.Page.ShouldBe(query.Page);
result.Count.ShouldBe(1);

tenant.Should().BeEquivalentTo(result.Items.First());
var checkingTenant = result.Items.First();

tenant.Id.ShouldBe(checkingTenant.Id);
tenant.Name.ShouldBe(checkingTenant.Name);
tenant.Users.Count.ShouldBe(checkingTenant.Users.Count());
}

[Fact]
Expand All @@ -51,10 +55,10 @@ public async Task Should_Not_Get_Deleted_Tenant()
new GetAllTenantsQuery(query, false),
default);

result.PageSize.Should().Be(query.PageSize);
result.Page.Should().Be(query.Page);
result.Count.Should().Be(0);
result.PageSize.ShouldBe(query.PageSize);
result.Page.ShouldBe(query.Page);
result.Count.ShouldBe(0);

result.Items.Should().HaveCount(0);
result.Items.Count.ShouldBe(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Application.Queries.Tenants.GetTenantById;
using CleanArchitecture.Application.Tests.Fixtures.Queries.Tenants;
using CleanArchitecture.Domain.Errors;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Application.Tests.Queries.Tenants;
Expand All @@ -22,7 +23,9 @@ public async Task Should_Get_Existing_Tenant()

_fixture.VerifyNoDomainNotification();

tenant.Should().BeEquivalentTo(result);
tenant.Id.ShouldBe(result!.Id);
tenant.Name.ShouldBe(result.Name);
tenant.Users.Count.ShouldBe(result.Users.Count());
}

[Fact]
Expand All @@ -38,6 +41,6 @@ public async Task Should_Not_Get_Deleted_Tenant()
nameof(GetTenantByIdQuery),
ErrorCodes.ObjectNotFound,
$"Tenant with id {tenant.Id} could not be found");
result.Should().BeNull();
result.ShouldBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using CleanArchitecture.Application.Queries.Users.GetAll;
using CleanArchitecture.Application.Tests.Fixtures.Queries.Users;
using CleanArchitecture.Application.ViewModels;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Application.Tests.Queries.Users;
Expand All @@ -29,14 +29,14 @@ public async Task Should_Get_All_Users()

_fixture.VerifyNoDomainNotification();

result.PageSize.Should().Be(query.PageSize);
result.Page.Should().Be(query.Page);
result.Count.Should().Be(1);
result.PageSize.ShouldBe(query.PageSize);
result.Page.ShouldBe(query.Page);
result.Count.ShouldBe(1);

var userViewModels = result.Items.ToArray();
userViewModels.Should().NotBeNull();
userViewModels.Should().ContainSingle();
userViewModels.FirstOrDefault()!.Id.Should().Be(_fixture.ExistingUserId);
userViewModels.ShouldNotBeNull();
userViewModels.ShouldHaveSingleItem();
userViewModels.FirstOrDefault()!.Id.ShouldBe(_fixture.ExistingUserId);
}

[Fact]
Expand All @@ -56,10 +56,10 @@ public async Task Should_Not_Get_Deleted_Users()

_fixture.VerifyNoDomainNotification();

result.PageSize.Should().Be(query.PageSize);
result.Page.Should().Be(query.Page);
result.Count.Should().Be(0);
result.PageSize.ShouldBe(query.PageSize);
result.Page.ShouldBe(query.Page);
result.Count.ShouldBe(0);

result.Items.Should().BeEmpty();
result.Items.ShouldBeEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using CleanArchitecture.Application.Queries.Users.GetUserById;
using CleanArchitecture.Application.Tests.Fixtures.Queries.Users;
using CleanArchitecture.Domain.Errors;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Application.Tests.Queries.Users;
Expand All @@ -23,8 +23,8 @@ public async Task Should_Get_Existing_User()

_fixture.VerifyNoDomainNotification();

result.Should().NotBeNull();
result!.Id.Should().Be(_fixture.ExistingUserId);
result.ShouldNotBeNull();
result!.Id.ShouldBe(_fixture.ExistingUserId);
}

[Fact]
Expand All @@ -42,7 +42,7 @@ public async Task Should_Raise_Notification_For_No_User()
ErrorCodes.ObjectNotFound,
$"User with id {request.Id} could not be found");

result.Should().BeNull();
result.ShouldBeNull();
}

[Fact]
Expand All @@ -59,6 +59,6 @@ public async Task Should_Not_Get_Deleted_User()
ErrorCodes.ObjectNotFound,
$"User with id {_fixture.ExistingUserId} could not be found");

result.Should().BeNull();
result.ShouldBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;
using CleanArchitecture.Domain.Commands.Users.LoginUser;
using CleanArchitecture.Domain.Errors;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Domain.Tests.CommandHandler.User.LoginUser;
Expand All @@ -25,25 +25,25 @@ public async Task Should_Login_User()

_fixture.VerifyNoDomainNotification();

token.Should().NotBeNullOrEmpty();
token.ShouldNotBeNullOrEmpty();

var handler = new JwtSecurityTokenHandler();
var decodedToken = handler.ReadToken(token) as JwtSecurityToken;

var userIdClaim = decodedToken!.Claims
.FirstOrDefault(x => string.Equals(x.Type, ClaimTypes.NameIdentifier));

Guid.Parse(userIdClaim!.Value).Should().Be(user.Id);
Guid.Parse(userIdClaim!.Value).ShouldBe(user.Id);

var userEmailClaim = decodedToken.Claims
.FirstOrDefault(x => string.Equals(x.Type, ClaimTypes.Email));

userEmailClaim!.Value.Should().Be(user.Email);
userEmailClaim!.Value.ShouldBe(user.Email);

var userRoleClaim = decodedToken.Claims
.FirstOrDefault(x => string.Equals(x.Type, ClaimTypes.Role));

userRoleClaim!.Value.Should().Be(user.Role.ToString());
userRoleClaim!.Value.ShouldBe(user.Role.ToString());
}

[Fact]
Expand All @@ -59,7 +59,7 @@ public async Task Should_Not_Login_User_No_User()
ErrorCodes.ObjectNotFound,
$"There is no user with email {command.Email}");

token.Should().BeEmpty();
token.ShouldBeEmpty();
}

[Fact]
Expand All @@ -77,6 +77,6 @@ public async Task Should_Not_Login_User_Incorrect_Password()
DomainErrorCodes.User.PasswordIncorrect,
"The password is incorrect");

token.Should().BeEmpty();
token.ShouldBeEmpty();
}
}
34 changes: 16 additions & 18 deletions CleanArchitecture.Domain.Tests/ValidationTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using CleanArchitecture.Domain.Commands;
using FluentAssertions;
using FluentValidation;
using Shouldly;

namespace CleanArchitecture.Domain.Tests;

Expand All @@ -21,8 +21,8 @@ protected void ShouldBeValid(TCommand command)
{
var result = _validation.Validate(command);

result.IsValid.Should().BeTrue();
result.Errors.Should().BeEmpty();
result.IsValid.ShouldBeTrue();
result.Errors.ShouldBeEmpty();
}

protected void ShouldHaveSingleError(
Expand All @@ -31,11 +31,11 @@ protected void ShouldHaveSingleError(
{
var result = _validation.Validate(command);

result.IsValid.Should().BeFalse();
result.IsValid.ShouldBeFalse();

result.Errors.Count.Should().Be(1);
result.Errors.Count.ShouldBe(1);

result.Errors.First().ErrorCode.Should().Be(expectedCode);
result.Errors.First().ErrorCode.ShouldBe(expectedCode);
}

protected void ShouldHaveSingleError(
Expand All @@ -45,12 +45,12 @@ protected void ShouldHaveSingleError(
{
var result = _validation.Validate(command);

result.IsValid.Should().BeFalse();
result.IsValid.ShouldBeFalse();

result.Errors.Count.Should().Be(1);
result.Errors.Count.ShouldBe(1);

result.Errors.First().ErrorCode.Should().Be(expectedCode);
result.Errors.First().ErrorMessage.Should().Be(expectedMessage);
result.Errors.First().ErrorCode.ShouldBe(expectedCode);
result.Errors.First().ErrorMessage.ShouldBe(expectedMessage);
}

protected void ShouldHaveExpectedErrors(
Expand All @@ -59,15 +59,14 @@ protected void ShouldHaveExpectedErrors(
{
var result = _validation.Validate(command);

result.IsValid.Should().BeFalse();
result.Errors.Count.Should().Be(expectedErrors.Length);
result.IsValid.ShouldBeFalse();
result.Errors.Count.ShouldBe(expectedErrors.Length);

foreach (var error in expectedErrors)
{
result.Errors
.Count(validation => validation.ErrorCode == error.Key && validation.ErrorMessage == error.Value)
.Should()
.Be(1);
.ShouldBe(1);
}
}

Expand All @@ -77,15 +76,14 @@ protected void ShouldHaveExpectedErrors(
{
var result = _validation.Validate(command);

result.IsValid.Should().BeFalse();
result.Errors.Count.Should().Be(expectedErrors.Length);
result.IsValid.ShouldBeFalse();
result.Errors.Count.ShouldBe(expectedErrors.Length);

foreach (var error in expectedErrors)
{
result.Errors
.Count(validation => validation.ErrorCode == error)
.Should()
.Be(1);
.ShouldBe(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using CleanArchitecture.Domain.Notifications;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace CleanArchitecture.Infrastructure.Tests;
Expand All @@ -10,7 +10,7 @@ public sealed class DomainNotificationHandlerTests
public void Should_Create_DomainNotificationHandler_Instance()
{
var domainNotificationHandler = new DomainNotificationHandler();
domainNotificationHandler.GetNotifications().Should().BeEmpty();
domainNotificationHandler.GetNotifications().ShouldBeEmpty();
}

[Fact]
Expand All @@ -23,7 +23,7 @@ public void Should_Handle_DomainNotification()
var domainNotification = new DomainNotification(key, value, code);
var domainNotificationHandler = new DomainNotificationHandler();
domainNotificationHandler.Handle(domainNotification);
domainNotificationHandler.GetNotifications().Should().HaveCount(1);
domainNotificationHandler.GetNotifications().Count.ShouldBe(1);
}

[Fact]
Expand All @@ -36,7 +36,7 @@ public void Should_Handle_DomainNotification_Overload()
var domainNotification = new DomainNotification(key, value, code);
var domainNotificationHandler = new DomainNotificationHandler();
domainNotificationHandler.Handle(domainNotification);
domainNotificationHandler.GetNotifications().Should().HaveCount(1);
domainNotificationHandler.GetNotifications().Count.ShouldBe(1);
}

[Fact]
Expand All @@ -50,14 +50,14 @@ public void DomainNotification_HasNotifications_After_Handling_One()
var domainNotificationHandler = new DomainNotificationHandler();
domainNotificationHandler.Handle(domainNotification);

domainNotificationHandler.HasNotifications().Should().BeTrue();
domainNotificationHandler.HasNotifications().ShouldBeTrue();
}

[Fact]
public void DomainNotification_HasNotifications_False_Not_Handling_One()
{
var domainNotificationHandler = new DomainNotificationHandler();

domainNotificationHandler.HasNotifications().Should().BeFalse();
domainNotificationHandler.HasNotifications().ShouldBeFalse();
}
}
Loading

0 comments on commit d31fc62

Please sign in to comment.