Skip to content

Commit a98d98a

Browse files
kamilbaczekkamilbaczek
authored andcommitted
refactor: update tests to use Shouldly assertions instead of FluentAssertions
1 parent ae94989 commit a98d98a

19 files changed

Lines changed: 86 additions & 118 deletions

File tree

Chapter-3-microservice-extraction/Fitnet.Common/Fitnet.Common.Api.UnitTests/ExceptionMiddlewareTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ namespace EvolutionaryArchitecture.Fitnet.Common.Api.UnitTests;
22

33
using ErrorHandling;
44
using Core.BusinessRules;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using System;
58

69
public sealed class ExceptionMiddlewareTests
710
{
@@ -19,10 +22,10 @@ internal async Task Given_business_rule_validation_exception_Then_returns_confli
1922
await middleware.InvokeAsync(_context);
2023

2124
// Assert
22-
_context.Response.StatusCode.Should().Be((int)HttpStatusCode.Conflict);
25+
_context.Response.StatusCode.ShouldBe((int)HttpStatusCode.Conflict);
2326

2427
var responseMessage = await GetExceptionResponseMessage();
25-
responseMessage.Should().Be(exceptionMessage);
28+
responseMessage.ShouldBe(exceptionMessage);
2629
}
2730

2831
[Fact]
@@ -37,10 +40,10 @@ internal async Task Given_other_than_business_rule_validation_exception_Then_ret
3740
await middleware.InvokeAsync(_context);
3841

3942
// Assert
40-
_context.Response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
43+
_context.Response.StatusCode.ShouldBe((int)HttpStatusCode.InternalServerError);
4144

4245
var responseMessage = await GetExceptionResponseMessage();
43-
responseMessage.Should().Be(exceptionMessage);
46+
responseMessage.ShouldBe(exceptionMessage);
4447
}
4548

4649
private static DefaultHttpContext GetHttpContext() =>

Chapter-3-microservice-extraction/Fitnet.Common/Fitnet.Common.Api.UnitTests/Fitnet.Common.Api.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="FluentAssertions" Version="6.12.2" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14+
<PackageReference Include="Shouldly" Version="4.3.0" />
1415
<PackageReference Include="xunit" Version="2.9.2" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1617
<PrivateAssets>all</PrivateAssets>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
global using System.Net;
22
global using Xunit;
3-
global using FluentAssertions;
43
global using Microsoft.AspNetCore.Http;
5-
global using Newtonsoft.Json;
4+
global using Newtonsoft.Json;
5+
global using Shouldly;

Chapter-3-microservice-extraction/Fitnet.Common/Fitnet.Common.Core.UnitTests/BusinessRulesEngine/BusinessRuleValidatorTests.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ internal void Given_concrete_business_rule_which_is_met_Then_validation_should_p
1010
// Arrange
1111

1212
// Act
13-
var act = () => BusinessRuleValidator.Validate(new FakeBusinessRule(20));
13+
static void FakeBusinessRule()
14+
{
15+
BusinessRuleValidator.Validate(new FakeBusinessRule(20));
16+
}
1417

1518
// Assert
16-
act.Should().NotThrow<BusinessRuleValidationException>();
19+
Should.NotThrow(FakeBusinessRule);
1720
}
1821

1922
[Fact]
@@ -22,9 +25,13 @@ internal void Given_concrete_business_rule_which_is_not_met_Then_validation_shou
2225
// Arrange
2326

2427
// Act
25-
var act = () => BusinessRuleValidator.Validate(new FakeBusinessRule(1));
28+
static void NotMetRuleAction()
29+
{
30+
BusinessRuleValidator.Validate(new FakeBusinessRule(1));
31+
}
2632

2733
// Assert
28-
act.Should().Throw<BusinessRuleValidationException>().WithMessage("Fake business rule was not met");
34+
var exception = Should.Throw<BusinessRuleValidationException>(NotMetRuleAction);
35+
exception.Message.ShouldBe("Fake business rule was not met");
2936
}
3037
}

Chapter-3-microservice-extraction/Fitnet.Common/Fitnet.Common.Core.UnitTests/Fitnet.Common.Core.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="FluentAssertions" Version="6.12.2" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14+
<PackageReference Include="Shouldly" Version="4.3.0" />
1415
<PackageReference Include="xunit" Version="2.9.2" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1617
<PrivateAssets>all</PrivateAssets>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
global using Shouldly;
12
global using Xunit;
2-
global using FluentAssertions;

Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Api.UnitTests/Fitnet.Contracts.Api.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<PackageReference Include="Bogus" Version="35.6.1" />
4-
<PackageReference Include="FluentAssertions" Version="6.12.2" />
54
<PackageReference Include="xunit" Version="2.9.2" />
65
<PackageReference Include="xunit.analyzers" Version="1.17.0">
76
<PrivateAssets>all</PrivateAssets>

Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Core.UnitTests/Fitnet.Contracts.Core.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Bogus" Version="35.6.1" />
99
<PackageReference Include="EvolutionaryArchitecture.Fitnet.Common.Core" Version="3.2.5" />
10-
<PackageReference Include="FluentAssertions" Version="6.12.2" />
10+
<PackageReference Include="Shouldly" Version="4.3.0" />
1111
<PackageReference Include="xunit" Version="2.9.2" />
1212
<PackageReference Include="xunit.analyzers" Version="1.17.0">
1313
<PrivateAssets>all</PrivateAssets>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
global using FluentAssertions;
1+
global using System;
2+
global using Shouldly;
23
global using Xunit;

Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Core.UnitTests/PrepareContract/BusinessRules/ContractCanBePreparedOnlyForAdultRuleTests.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,31 @@ public sealed class ContractCanBePreparedOnlyForAdultRuleTests
99
internal void Given_customer_age_which_is_less_than_18_Then_validation_should_throw()
1010
{
1111
// Arrange
12+
const int minorAge = 17;
1213

13-
// Act
14-
var act = () => BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(17));
15-
16-
// Assert
17-
act.Should().Throw<BusinessRuleValidationException>().WithMessage("Contract can not be prepared for a person who is not adult");
14+
// Act & Assert
15+
var exception = Should.Throw<BusinessRuleValidationException>(() =>
16+
BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(minorAge)));
17+
exception.Message.ShouldBe("Contract can not be prepared for a person who is not adult");
1818
}
1919

2020
[Fact]
2121
internal void Given_customer_age_which_is_equal_to_18_Then_validation_should_pass()
2222
{
2323
// Arrange
24+
const int exactLegalAge = 18;
2425

25-
// Act
26-
var act = () => BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(18));
27-
28-
// Assert
29-
act.Should().NotThrow<BusinessRuleValidationException>();
26+
// Act & Assert
27+
Should.NotThrow(() => BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(exactLegalAge)));
3028
}
3129

3230
[Fact]
3331
internal void Given_customer_age_which_is_greater_than_18_Then_validation_should_pass()
3432
{
3533
// Arrange
34+
const int adultAge = 19;
3635

37-
// Act
38-
var act = () => BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(19));
39-
40-
// Assert
41-
act.Should().NotThrow<BusinessRuleValidationException>();
36+
// Act & Assert
37+
Should.NotThrow(() => BusinessRuleValidator.Validate(new ContractCanBePreparedOnlyForAdultRule(adultAge)));
4238
}
4339
}

0 commit comments

Comments
 (0)