Skip to content

Commit 63b83a6

Browse files
committed
refactor(tests)!: simplify mock setup
1 parent 61649ef commit 63b83a6

File tree

3 files changed

+90
-73
lines changed

3 files changed

+90
-73
lines changed

Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99

1010
namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Unit;
1111

12-
public class PlayerControllerTests
12+
public class PlayerControllerTests : IDisposable
1313
{
14+
private bool _disposed;
15+
16+
public PlayerControllerTests()
17+
{
18+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
19+
}
20+
1421
/* -------------------------------------------------------------------------
1522
* HTTP POST
1623
* ---------------------------------------------------------------------- */
@@ -20,10 +27,9 @@ public class PlayerControllerTests
2027
public async Task GivenPostAsync_WhenModelStateIsInvalid_ThenResponseStatusCodeShouldBe400BadRequest()
2128
{
2229
// Arrange
23-
var service = new Mock<IPlayerService>();
24-
var logger = PlayerMocks.LoggerMock<PlayersController>();
30+
var (service, logger) = PlayerMocks.SetupControllerMocks();
2531

26-
var controller = new PlayersController(service.Object, logger.Object);
32+
var controller = new PlayerController(service.Object, logger.Object);
2733
controller.ModelState.Merge(PlayerStubs.CreateModelError("FirstName", "Required"));
2834

2935
// Act
@@ -40,11 +46,10 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenR
4046
{
4147
// Arrange
4248
var player = PlayerFakes.CreateOneByIdFromStarting11(10);
43-
var service = new Mock<IPlayerService>();
49+
var (service, logger) = PlayerMocks.SetupControllerMocks();
4450
service.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>())).ReturnsAsync(player);
45-
var logger = PlayerMocks.LoggerMock<PlayersController>();
4651

47-
var controller = new PlayersController(service.Object, logger.Object);
52+
var controller = new PlayerController(service.Object, logger.Object);
4853

4954
// Act
5055
var response = await controller.PostAsync(player) as Conflict;
@@ -61,15 +66,13 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenRes
6166
{
6267
// Arrange
6368
var player = PlayerFakes.CreateOneNew();
64-
65-
var service = new Mock<IPlayerService>();
69+
var (service, logger) = PlayerMocks.SetupControllerMocks();
6670
service
6771
.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>()))
6872
.ReturnsAsync(null as Player);
6973
service.Setup(service => service.CreateAsync(It.IsAny<Player>()));
70-
var logger = PlayerMocks.LoggerMock<PlayersController>();
7174

72-
var controller = new PlayersController(service.Object, logger.Object)
75+
var controller = new PlayerController(service.Object, logger.Object)
7376
{
7477
Url = PlayerMocks.UrlHelperMock().Object,
7578
};
@@ -94,11 +97,10 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsListOfPlayers_The
9497
{
9598
// Arrange
9699
var players = PlayerFakes.CreateStarting11();
97-
var service = new Mock<IPlayerService>();
100+
var (service, logger) = PlayerMocks.SetupControllerMocks();
98101
service.Setup(service => service.RetrieveAsync()).ReturnsAsync(players);
99-
var logger = PlayerMocks.LoggerMock<PlayersController>();
100102

101-
var controller = new PlayersController(service.Object, logger.Object);
103+
var controller = new PlayerController(service.Object, logger.Object);
102104

103105
// Act
104106
var response = await controller.GetAsync() as Ok<List<Player>>;
@@ -117,11 +119,10 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsEmptyList_ThenRes
117119
{
118120
// Arrange
119121
var players = new List<Player>(); // Count = 0
120-
var service = new Mock<IPlayerService>();
122+
var (service, logger) = PlayerMocks.SetupControllerMocks();
121123
service.Setup(service => service.RetrieveAsync()).ReturnsAsync(players);
122-
var logger = PlayerMocks.LoggerMock<PlayersController>();
123124

124-
var controller = new PlayersController(service.Object, logger.Object);
125+
var controller = new PlayerController(service.Object, logger.Object);
125126

126127
// Act
127128
var response = await controller.GetAsync() as NotFound;
@@ -137,13 +138,12 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsEmptyList_ThenRes
137138
public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound()
138139
{
139140
// Arrange
140-
var service = new Mock<IPlayerService>();
141+
var (service, logger) = PlayerMocks.SetupControllerMocks();
141142
service
142143
.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>()))
143144
.ReturnsAsync(null as Player);
144-
var logger = PlayerMocks.LoggerMock<PlayersController>();
145145

146-
var controller = new PlayersController(service.Object, logger.Object);
146+
var controller = new PlayerController(service.Object, logger.Object);
147147

148148
// Act
149149
var response = await controller.GetByIdAsync(It.IsAny<long>()) as NotFound;
@@ -160,11 +160,10 @@ public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_Th
160160
{
161161
// Arrange
162162
var player = PlayerFakes.CreateOneByIdFromStarting11(10);
163-
var service = new Mock<IPlayerService>();
163+
var (service, logger) = PlayerMocks.SetupControllerMocks();
164164
service.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>())).ReturnsAsync(player);
165-
var logger = PlayerMocks.LoggerMock<PlayersController>();
166165

167-
var controller = new PlayersController(service.Object, logger.Object);
166+
var controller = new PlayerController(service.Object, logger.Object);
168167

169168
// Act
170169
var response = await controller.GetByIdAsync(It.IsAny<long>()) as Ok<Player>;
@@ -182,13 +181,12 @@ public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_Th
182181
public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound()
183182
{
184183
// Arrange
185-
var service = new Mock<IPlayerService>();
184+
var (service, logger) = PlayerMocks.SetupControllerMocks();
186185
service
187186
.Setup(service => service.RetrieveBySquadNumberAsync(It.IsAny<int>()))
188187
.ReturnsAsync(null as Player);
189-
var logger = PlayerMocks.LoggerMock<PlayersController>();
190188

191-
var controller = new PlayersController(service.Object, logger.Object);
189+
var controller = new PlayerController(service.Object, logger.Object);
192190

193191
// Act
194192
var response = await controller.GetBySquadNumberAsync(It.IsAny<int>()) as NotFound;
@@ -208,13 +206,12 @@ public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsy
208206
{
209207
// Arrange
210208
var player = PlayerFakes.CreateOneByIdFromStarting11(10);
211-
var service = new Mock<IPlayerService>();
209+
var (service, logger) = PlayerMocks.SetupControllerMocks();
212210
service
213211
.Setup(service => service.RetrieveBySquadNumberAsync(It.IsAny<int>()))
214212
.ReturnsAsync(player);
215-
var logger = PlayerMocks.LoggerMock<PlayersController>();
216213

217-
var controller = new PlayersController(service.Object, logger.Object);
214+
var controller = new PlayerController(service.Object, logger.Object);
218215

219216
// Act
220217
var response = await controller.GetBySquadNumberAsync(It.IsAny<int>()) as Ok<Player>;
@@ -239,10 +236,9 @@ public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsy
239236
public async Task GivenPutAsync_WhenModelStateIsInvalid_ThenResponseStatusCodeShouldBe400BadRequest()
240237
{
241238
// Arrange
242-
var service = new Mock<IPlayerService>();
243-
var logger = PlayerMocks.LoggerMock<PlayersController>();
239+
var (service, logger) = PlayerMocks.SetupControllerMocks();
244240

245-
var controller = new PlayersController(service.Object, logger.Object);
241+
var controller = new PlayerController(service.Object, logger.Object);
246242
controller.ModelState.Merge(PlayerStubs.CreateModelError("FirstName", "Required"));
247243

248244
// Act
@@ -263,9 +259,9 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResp
263259
service
264260
.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>()))
265261
.ReturnsAsync(null as Player);
266-
var logger = PlayerMocks.LoggerMock<PlayersController>();
262+
var logger = PlayerMocks.LoggerMock<PlayerController>();
267263

268-
var controller = new PlayersController(service.Object, logger.Object);
264+
var controller = new PlayerController(service.Object, logger.Object);
269265

270266
// Act
271267
var response = await controller.PutAsync(It.IsAny<long>(), It.IsAny<Player>()) as NotFound;
@@ -283,12 +279,11 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenRe
283279
// Arrange
284280
var id = 10;
285281
var player = PlayerFakes.CreateOneByIdFromStarting11(id);
286-
var service = new Mock<IPlayerService>();
282+
var (service, logger) = PlayerMocks.SetupControllerMocks();
287283
service.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>())).ReturnsAsync(player);
288284
service.Setup(service => service.UpdateAsync(It.IsAny<Player>()));
289-
var logger = PlayerMocks.LoggerMock<PlayersController>();
290285

291-
var controller = new PlayersController(service.Object, logger.Object);
286+
var controller = new PlayerController(service.Object, logger.Object);
292287

293288
// Act
294289
var response = await controller.PutAsync(id, player) as NoContent;
@@ -309,13 +304,12 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenRe
309304
public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound()
310305
{
311306
// Arrange
312-
var service = new Mock<IPlayerService>();
307+
var (service, logger) = PlayerMocks.SetupControllerMocks();
313308
service
314309
.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>()))
315310
.ReturnsAsync(null as Player);
316-
var logger = PlayerMocks.LoggerMock<PlayersController>();
317311

318-
var controller = new PlayersController(service.Object, logger.Object);
312+
var controller = new PlayerController(service.Object, logger.Object);
319313

320314
// Act
321315
var response = await controller.DeleteAsync(It.IsAny<long>()) as NotFound;
@@ -332,12 +326,11 @@ public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_The
332326
{
333327
// Arrange
334328
var player = PlayerFakes.CreateOneByIdFromStarting11(10);
335-
var service = new Mock<IPlayerService>();
329+
var (service, logger) = PlayerMocks.SetupControllerMocks();
336330
service.Setup(service => service.RetrieveByIdAsync(It.IsAny<long>())).ReturnsAsync(player);
337331
service.Setup(service => service.DeleteAsync(It.IsAny<long>()));
338-
var logger = PlayerMocks.LoggerMock<PlayersController>();
339332

340-
var controller = new PlayersController(service.Object, logger.Object);
333+
var controller = new PlayerController(service.Object, logger.Object);
341334

342335
// Act
343336
var response = await controller.DeleteAsync(It.IsAny<long>()) as NoContent;
@@ -348,4 +341,19 @@ public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_The
348341
response.Should().NotBeNull().And.BeOfType<NoContent>();
349342
response?.StatusCode.Should().Be(StatusCodes.Status204NoContent);
350343
}
344+
345+
protected virtual void Dispose(bool disposing)
346+
{
347+
if (!_disposed)
348+
{
349+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
350+
_disposed = true;
351+
}
352+
}
353+
354+
public void Dispose()
355+
{
356+
Dispose(true);
357+
GC.SuppressFinalize(this);
358+
}
351359
}

Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public PlayerServiceTests()
2525
public async Task GivenCreateAsync_WhenRepositoryAddAsync_ThenAddsPlayerToRepositoryAndRemovesMemoryCache()
2626
{
2727
// Arrange
28-
var repository = PlayerMocks.RepositoryMock();
29-
var logger = PlayerMocks.LoggerMock<PlayerService>();
30-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
28+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
3129

3230
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
3331

@@ -49,10 +47,8 @@ public async Task GivenRetrieveAsync_WhenRepositoryGetAllAsyncReturnsPlayers_The
4947
{
5048
// Arrange
5149
var players = PlayerFakes.CreateStarting11();
52-
var repository = PlayerMocks.RepositoryMock();
50+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
5351
repository.Setup(repository => repository.GetAllAsync()).ReturnsAsync(players);
54-
var logger = PlayerMocks.LoggerMock<PlayerService>();
55-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
5652
var value = It.IsAny<object>();
5753

5854
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
@@ -73,9 +69,7 @@ public async Task GivenRetrieveAsync_WhenExecutedForTheSecondTime_ThenSecondExec
7369
{
7470
// Arrange
7571
var players = PlayerFakes.CreateStarting11();
76-
var repository = PlayerMocks.RepositoryMock();
77-
var logger = PlayerMocks.LoggerMock<PlayerService>();
78-
var memoryCache = PlayerMocks.MemoryCacheMock(players);
72+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks(cacheValue: players);
7973
var value = It.IsAny<object>();
8074

8175
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
@@ -98,10 +92,8 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsNull_
9892
{
9993
// Arrange
10094
var id = 999;
101-
var repository = PlayerMocks.RepositoryMock();
95+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
10296
repository.Setup(repository => repository.FindByIdAsync(id)).ReturnsAsync((Player?)null);
103-
var logger = PlayerMocks.LoggerMock<PlayerService>();
104-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
10597

10698
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
10799

@@ -119,10 +111,8 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsPlaye
119111
{
120112
// Arrange
121113
var player = PlayerFakes.CreateOneByIdFromStarting11(9);
122-
var repository = PlayerMocks.RepositoryMock();
114+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
123115
repository.Setup(repository => repository.FindByIdAsync(player.Id)).ReturnsAsync(player);
124-
var logger = PlayerMocks.LoggerMock<PlayerService>();
125-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
126116

127117
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
128118

@@ -141,12 +131,10 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe
141131
{
142132
// Arrange
143133
var squadNumber = 999;
144-
var repository = PlayerMocks.RepositoryMock();
134+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
145135
repository
146136
.Setup(repository => repository.FindBySquadNumberAsync(squadNumber))
147137
.ReturnsAsync((Player?)null);
148-
var logger = PlayerMocks.LoggerMock<PlayerService>();
149-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
150138

151139
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
152140

@@ -167,12 +155,10 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe
167155
{
168156
// Arrange
169157
var player = PlayerFakes.CreateOneByIdFromStarting11(9);
170-
var repository = PlayerMocks.RepositoryMock();
158+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
171159
repository
172160
.Setup(repository => repository.FindBySquadNumberAsync(player.SquadNumber))
173161
.ReturnsAsync(player);
174-
var logger = PlayerMocks.LoggerMock<PlayerService>();
175-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
176162

177163
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
178164

@@ -198,10 +184,8 @@ public async Task GivenUpdateAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_Then
198184
{
199185
// Arrange
200186
var player = PlayerFakes.CreateOneByIdFromStarting11(9);
201-
var repository = PlayerMocks.RepositoryMock();
187+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
202188
repository.Setup(repository => repository.FindByIdAsync(player.Id)).ReturnsAsync(player);
203-
var logger = PlayerMocks.LoggerMock<PlayerService>();
204-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
205189

206190
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
207191

@@ -224,10 +208,8 @@ public async Task GivenDeleteAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_Then
224208
{
225209
// Arrange
226210
var player = PlayerFakes.CreateOneNew();
227-
var repository = PlayerMocks.RepositoryMock();
211+
var (repository, logger, memoryCache) = PlayerMocks.SetupServiceMocks();
228212
repository.Setup(repository => repository.FindByIdAsync(player.Id)).ReturnsAsync(player);
229-
var logger = PlayerMocks.LoggerMock<PlayerService>();
230-
var memoryCache = PlayerMocks.MemoryCacheMock(It.IsAny<object>());
231213

232214
var service = new PlayerService(repository.Object, logger.Object, memoryCache.Object);
233215

0 commit comments

Comments
 (0)