99
1010namespace 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}
0 commit comments