Skip to content

Commit 2ee52b0

Browse files
committed
refactor(tests): refactor UnitTests to xunit
1 parent d1fcc29 commit 2ee52b0

File tree

8 files changed

+80
-69
lines changed

8 files changed

+80
-69
lines changed

Directory.Packages.props

+9
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,14 @@
2020
<PackageVersion Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
2121
<!-- Analyzers -->
2222
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
23+
<!-- Tests -->
24+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
25+
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
26+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
27+
<PackageVersion Include="throw" Version="1.4.0" />
28+
<PackageVersion Include="xunit" Version="2.6.5" />
29+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
30+
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
31+
<PackageVersion Include="Moq" Version="4.16.1" />
2332
</ItemGroup>
2433
</Project>

VerticalSliceArchitecture.sln

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
README.md = README.md
1515
EndProjectSection
1616
EndProject
17+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{5F44F0AC-5482-4A73-9CAE-6A550E23B510}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application.UnitTests", "tests\Application.UnitTests\Application.UnitTests.csproj", "{24BF57AE-47C4-4065-BAB0-72ACB562D22E}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
@@ -32,13 +36,18 @@ Global
3236
{22454EE9-16B3-4B7A-8352-37E59B858155}.Debug|Any CPU.Build.0 = Debug|Any CPU
3337
{22454EE9-16B3-4B7A-8352-37E59B858155}.Release|Any CPU.ActiveCfg = Release|Any CPU
3438
{22454EE9-16B3-4B7A-8352-37E59B858155}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{24BF57AE-47C4-4065-BAB0-72ACB562D22E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{24BF57AE-47C4-4065-BAB0-72ACB562D22E}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{24BF57AE-47C4-4065-BAB0-72ACB562D22E}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{24BF57AE-47C4-4065-BAB0-72ACB562D22E}.Release|Any CPU.Build.0 = Release|Any CPU
3543
EndGlobalSection
3644
GlobalSection(SolutionProperties) = preSolution
3745
HideSolutionNode = FALSE
3846
EndGlobalSection
3947
GlobalSection(NestedProjects) = preSolution
4048
{34C0FACD-F3D9-400C-8945-554DD6B0819A} = {6ED356A7-8B47-4613-AD01-C85CF28491BD}
4149
{22454EE9-16B3-4B7A-8352-37E59B858155} = {6ED356A7-8B47-4613-AD01-C85CF28491BD}
50+
{24BF57AE-47C4-4065-BAB0-72ACB562D22E} = {5F44F0AC-5482-4A73-9CAE-6A550E23B510}
4251
EndGlobalSection
4352
GlobalSection(ExtensibilityGlobals) = postSolution
4453
SolutionGuid = {3CB609D9-5D54-4C11-A371-DAAC8B74E430}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
5-
<RootNamespace>VerticalSliceArchitecture.Application.UnitTests</RootNamespace>
6-
<AssemblyName>VerticalSliceArchitecture.Application.UnitTests</AssemblyName>
3+
<PropertyGroup>
4+
<IsPackable>false</IsPackable>
5+
<IsTestProject>true</IsTestProject>
6+
</PropertyGroup>
77

8-
<IsPackable>false</IsPackable>
9-
<ImplicitUsings>enable</ImplicitUsings>
10-
<Nullable>enable</Nullable>
11-
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="FluentAssertions" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
11+
<PackageReference Include="xunit" />
12+
<PackageReference Include="xunit.runner.visualstudio">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
<PackageReference Include="Moq" />
21+
</ItemGroup>
1222

13-
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
15-
<PackageReference Include="nunit" Version="3.13.2" />
16-
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0">
17-
<PrivateAssets>all</PrivateAssets>
18-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19-
</PackageReference>
20-
<PackageReference Include="FluentAssertions" Version="6.2.0" />
21-
<PackageReference Include="Moq" Version="4.16.1" />
22-
</ItemGroup>
23-
24-
<ItemGroup>
23+
<ItemGroup>
2524
<ProjectReference Include="..\..\src\Application\Application.csproj" />
2625
</ItemGroup>
2726

2827
</Project>
28+
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
using VerticalSliceArchitecture.Application.Common.Behaviours;
2-
using VerticalSliceArchitecture.Application.Common.Interfaces;
3-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
2+
43
using Moq;
5-
using NUnit.Framework;
4+
5+
using VerticalSliceArchitecture.Application.Common.Behaviours;
6+
using VerticalSliceArchitecture.Application.Common.Interfaces;
67
using VerticalSliceArchitecture.Application.Features.TodoItems;
78

89
namespace VerticalSliceArchitecture.Application.UnitTests.Common.Behaviours;
910

1011
public class RequestLoggerTests
1112
{
12-
private Mock<ILogger<CreateTodoItemCommand>> _logger = null!;
13-
private Mock<ICurrentUserService> _currentUserService = null!;
13+
private readonly Mock<ILogger<CreateTodoItemCommand>> _logger;
14+
private readonly Mock<ICurrentUserService> _currentUserService;
1415

15-
[SetUp]
16-
public void Setup()
16+
public RequestLoggerTests()
1717
{
1818
_logger = new Mock<ILogger<CreateTodoItemCommand>>();
1919
_currentUserService = new Mock<ICurrentUserService>();
2020
}
2121

22-
[Test]
22+
[Fact]
2323
public async Task ShouldCallGetUserNameAsyncOnceIfAuthenticated()
2424
{
2525
_currentUserService.Setup(x => x.UserId).Returns(Guid.NewGuid().ToString());
2626

2727
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _currentUserService.Object);
2828

29-
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, new CancellationToken());
29+
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, CancellationToken.None);
3030
}
3131

32-
[Test]
32+
[Fact]
3333
public async Task ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated()
3434
{
3535
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _currentUserService.Object);
3636

37-
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, new CancellationToken());
37+
await requestLogger.Process(new CreateTodoItemCommand { ListId = 1, Title = "title" }, CancellationToken.None);
3838
}
3939
}

tests/Application.UnitTests/Common/Exceptions/ValidationExceptionTests.cs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
using VerticalSliceArchitecture.Application.Common.Exceptions;
2-
using FluentAssertions;
3-
using FluentValidation.Results;
4-
using NUnit.Framework;
1+
using FluentValidation.Results;
2+
3+
using VerticalSliceArchitecture.Application.Common.Exceptions;
54

65
namespace VerticalSliceArchitecture.Application.UnitTests.Common.Exceptions;
76

87
public class ValidationExceptionTests
98
{
10-
[Test]
9+
[Fact]
1110
public void DefaultConstructorCreatesAnEmptyErrorDictionary()
1211
{
1312
var actual = new ValidationException().Errors;
1413

1514
actual.Keys.Should().BeEquivalentTo(Array.Empty<string>());
1615
}
1716

18-
[Test]
17+
[Fact]
1918
public void SingleValidationFailureCreatesASingleElementErrorDictionary()
2019
{
2120
var failures = new List<ValidationFailure>
2221
{
23-
new ValidationFailure("Age", "must be over 18"),
22+
new("Age", "must be over 18"),
2423
};
2524

2625
var actual = new ValidationException(failures).Errors;
@@ -29,17 +28,17 @@ public void SingleValidationFailureCreatesASingleElementErrorDictionary()
2928
actual["Age"].Should().BeEquivalentTo(new string[] { "must be over 18" });
3029
}
3130

32-
[Test]
31+
[Fact]
3332
public void MulitpleValidationFailureForMultiplePropertiesCreatesAMultipleElementErrorDictionaryEachWithMultipleValues()
3433
{
3534
var failures = new List<ValidationFailure>
3635
{
37-
new ValidationFailure("Age", "must be 18 or older"),
38-
new ValidationFailure("Age", "must be 25 or younger"),
39-
new ValidationFailure("Password", "must contain at least 8 characters"),
40-
new ValidationFailure("Password", "must contain a digit"),
41-
new ValidationFailure("Password", "must contain upper case letter"),
42-
new ValidationFailure("Password", "must contain lower case letter"),
36+
new("Age", "must be 18 or older"),
37+
new("Age", "must be 25 or younger"),
38+
new("Password", "must contain at least 8 characters"),
39+
new("Password", "must contain a digit"),
40+
new("Password", "must contain upper case letter"),
41+
new("Password", "must contain lower case letter"),
4342
};
4443

4544
var actual = new ValidationException(failures).Errors;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.Runtime.Serialization;
2-
using AutoMapper;
1+
using AutoMapper;
2+
33
using VerticalSliceArchitecture.Application.Common.Mappings;
4-
using NUnit.Framework;
54
using VerticalSliceArchitecture.Application.Domain.Todos;
65
using VerticalSliceArchitecture.Application.Features.TodoLists;
76

@@ -20,28 +19,24 @@ public MappingTests()
2019
_mapper = _configuration.CreateMapper();
2120
}
2221

23-
[Test]
22+
[Fact]
2423
public void ShouldHaveValidConfiguration()
2524
{
2625
_configuration.AssertConfigurationIsValid();
2726
}
2827

29-
[Test]
30-
[TestCase(typeof(TodoList), typeof(TodoListDto))]
31-
[TestCase(typeof(TodoItem), typeof(TodoItemDto))]
28+
[Theory]
29+
[InlineData(typeof(TodoList), typeof(TodoListDto))]
30+
[InlineData(typeof(TodoItem), typeof(TodoItemDto))]
3231
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
3332
{
3433
var instance = GetInstanceOf(source);
3534

3635
_mapper.Map(instance, source, destination);
3736
}
3837

39-
private object GetInstanceOf(Type type)
38+
private static object GetInstanceOf(Type type)
4039
{
41-
if (type.GetConstructor(Type.EmptyTypes) != null)
42-
return Activator.CreateInstance(type)!;
43-
44-
// Type without parameterless constructor
45-
return FormatterServices.GetUninitializedObject(type);
40+
return Activator.CreateInstance(type)!;
4641
}
4742
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using FluentAssertions;
2+
3+
global using Xunit;

tests/Application.UnitTests/ValueObjects/ColourTests.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using FluentAssertions;
2-
3-
using NUnit.Framework;
4-
5-
using VerticalSliceArchitecture.Application.Common.Exceptions;
1+
using VerticalSliceArchitecture.Application.Common.Exceptions;
62
using VerticalSliceArchitecture.Application.Domain.ValueObjects;
73

84
namespace VerticalSliceArchitecture.Application.UnitTests.ValueObjects;
95

106
public class ColourTests
117
{
12-
[Test]
8+
[Fact]
139
public void ShouldReturnCorrectColourCode()
1410
{
1511
var code = "#FFFFFF";
@@ -19,31 +15,31 @@ public void ShouldReturnCorrectColourCode()
1915
colour.Code.Should().Be(code);
2016
}
2117

22-
[Test]
18+
[Fact]
2319
public void ToStringReturnsCode()
2420
{
2521
var colour = Colour.White;
2622

2723
colour.ToString().Should().Be(colour.Code);
2824
}
2925

30-
[Test]
26+
[Fact]
3127
public void ShouldPerformImplicitConversionToColourCodeString()
3228
{
3329
string code = Colour.White;
3430

3531
code.Should().Be("#FFFFFF");
3632
}
3733

38-
[Test]
34+
[Fact]
3935
public void ShouldPerformExplicitConversionGivenSupportedColourCode()
4036
{
4137
var colour = (Colour)"#FFFFFF";
4238

4339
colour.Should().Be(Colour.White);
4440
}
4541

46-
[Test]
42+
[Fact]
4743
public void ShouldThrowUnsupportedColourExceptionGivenNotSupportedColourCode()
4844
{
4945
FluentActions.Invoking(() => Colour.From("##FF33CC"))

0 commit comments

Comments
 (0)