Skip to content

Commit e6e4e98

Browse files
committed
MoQ -> NSubstitute, FluentAssertions -> Shouldly, AutoMock -> AutoFixture
1 parent 021aeea commit e6e4e98

File tree

6 files changed

+34
-40
lines changed

6 files changed

+34
-40
lines changed

09. testing/Site.Tests/Fixtures/SiteApp.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
namespace Site.Tests.Fixtures;
22

3-
public class SiteApp : WebApplicationFactory<Program>
3+
public class SiteApp(string environment = "Development") : WebApplicationFactory<Program>
44
{
5-
private readonly string _environment;
6-
7-
public SiteApp(string environment = "Development")
8-
{
9-
_environment = environment;
10-
}
11-
125
protected override IHost CreateHost(IHostBuilder builder)
136
{
14-
builder.UseEnvironment(_environment);
7+
builder.UseEnvironment(environment);
158

169
builder.ConfigureServices(services =>
1710
{

09. testing/Site.Tests/Global.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
global using FluentAssertions;
2-
global using FluentAssertions.Execution;
31
global using Microsoft.AspNetCore.Mvc.Testing;
42
global using Microsoft.Extensions.Hosting;
53
global using Microsoft.Extensions.Logging;
6-
global using Moq;
7-
global using Moq.AutoMock;
8-
global using Site;
9-
global using Site.Tests.Fixtures;
104
global using System.Text;
115
global using System.Text.Json;
6+
global using AutoFixture;
7+
global using AutoFixture.AutoNSubstitute;
8+
global using NSubstitute;
9+
global using Shouldly;
10+
global using Xunit;
11+
global using Site;
12+
global using Site.Tests.Fixtures;
1213
global using Site.Models;
1314
global using Site.Controllers;
14-
global using Xunit;

09. testing/Site.Tests/MinimalIntegrationTests.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@ public async Task Get_SaysHello()
1212
{
1313
// arrange
1414
await using var application = new SiteApp();
15-
using var client = application.CreateClient();
15+
using HttpClient client = application.CreateClient();
1616

1717
// act
1818
using var res = await client.GetAsync("/api/minimal?name=test");
1919
var body = await res.Content.ReadAsStringAsync();
2020
MinimalModel? model = JsonSerializer.Deserialize<MinimalModel>(body, jsonOptions);
2121

2222
// assert
23-
using var scope = new AssertionScope();
24-
model.Should().NotBeNull();
23+
model.ShouldNotBeNull();
2524
ArgumentNullException.ThrowIfNull(model);
26-
model.Id.Should().Be(3);
27-
model.Message.Should().Be("Hello test");
25+
model.Id.ShouldBe(3);
26+
model.Message.ShouldBe("Hello test");
2827
}
2928

3029
[Fact]
@@ -47,11 +46,10 @@ public async Task Post_SaysHello()
4746
MinimalModel? model = JsonSerializer.Deserialize<MinimalModel>(body, jsonOptions);
4847

4948
// assert
50-
using var scope = new AssertionScope();
51-
model.Should().NotBeNull();
49+
model.ShouldNotBeNull();
5250
ArgumentNullException.ThrowIfNull(model);
53-
model.Message.Should().Be("Hello foo and 5");
54-
model.Data.Should().BeEquivalentTo(expected);
51+
model.Message.ShouldBe("Hello foo and 5");
52+
model.Data.ShouldBeEquivalentTo(expected);
5553
}
5654

5755
}

09. testing/Site.Tests/Site.Tests.csproj

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="FluentAssertions" Version="8.0.1" />
11+
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.1" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14-
<PackageReference Include="Moq" Version="4.20.72" />
15-
<PackageReference Include="Moq.AutoMock" Version="3.5.0" />
14+
<PackageReference Include="NSubstitute" Version="5.3.0" />
15+
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Shouldly" Version="4.3.0" />
1620
<PackageReference Include="xunit" Version="2.9.3" />
1721
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1822
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

09. testing/Site.Tests/WeatherIntegrationTests.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ public async Task Gets5Forecasts()
1515
List<WeatherForecast>? weatherForecasts = JsonSerializer.Deserialize<List<WeatherForecast>>(body);
1616

1717
// assert
18-
using var scope = new AssertionScope();
19-
weatherForecasts.Should().NotBeNull();
20-
weatherForecasts.Should().HaveCount(5);
18+
weatherForecasts.ShouldNotBeNull();
19+
weatherForecasts.Count.ShouldBe(5);
2120
}
2221

2322
}

09. testing/Site.Tests/WeatherUnitTests.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ public class WeatherUnitTests
55
[Fact]
66
public void Gets5Forecasts()
77
{
8-
// arrange
98
/*
10-
var logger = new Mock<ILogger<WeatherForecastController>>();
11-
WeatherForecastController controller = new WeatherForecastController(logger.Object);
9+
// arrange
10+
var logger = Substitute.For<ILogger<WeatherForecastController>>();
11+
WeatherForecastController controller = new WeatherForecastController(logger);
1212
*/
13-
// use AutoMocker https://github.com/moq/Moq.AutoMocker
14-
var mocker = new AutoMocker();
15-
WeatherForecastController controller = mocker.CreateInstance<WeatherForecastController>();
13+
// use AutoFixture https://www.nuget.org/packages/AutoFixture.AutoNSubstitute
14+
var ioc = new Fixture().Customize(new AutoNSubstituteCustomization());
15+
//WeatherForecastController controller = ioc.Create<WeatherForecastController>();
16+
WeatherForecastController controller = ioc.Build<WeatherForecastController>().OmitAutoProperties().Create();
1617

1718
// act
1819
IEnumerable<WeatherForecast> weatherForecasts = controller.Get();
1920

2021
// assert
21-
using var scope = new AssertionScope();
22-
weatherForecasts.Should().NotBeNull();
23-
weatherForecasts.Should().HaveCount(5);
22+
weatherForecasts.ShouldNotBeNull();
23+
weatherForecasts.Count().ShouldBe(5);
2424
}
2525

2626
}

0 commit comments

Comments
 (0)