diff --git a/src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs b/src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs index 40041d5..b1ac927 100644 --- a/src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs +++ b/src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs @@ -6,7 +6,7 @@ namespace Dotnet.Samples.AspNetCore.WebApi.Data; public static class PlayerData { - public static List CreateStarting11() + public static List MakeStarting11() { return [ diff --git a/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs b/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs index e3fcab0..c9e7783 100644 --- a/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs +++ b/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs @@ -21,7 +21,7 @@ public static async Task SeedDbContextAsync(this IApplicationBuilder app) if (!await dbContext.Players.AnyAsync()) { - await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11()); + await dbContext.Players.AddRangeAsync(PlayerData.MakeStarting11()); await dbContext.SaveChangesAsync(); logger.LogInformation("Successfully seeded database with initial data."); } diff --git a/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs b/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs index b43b5af..438050d 100644 --- a/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs +++ b/src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs @@ -35,7 +35,7 @@ CancellationToken cancellationToken { if (!await dbContext.Players.AnyAsync(cancellationToken)) { - await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken); + await dbContext.Players.AddRangeAsync(PlayerData.MakeStarting11(), cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); } } diff --git a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs index 8fb4252..530d688 100644 --- a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs +++ b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs @@ -27,10 +27,10 @@ public PlayerControllerTests() public async Task GivenPostAsync_WhenValidatorReturnsErrors_ThenResponseStatusCodeShouldBe400BadRequest() { // Arrange - var payload = PlayerFakes.CreateRequestModelForOneNew(); + var request = PlayerFakes.MakeRequestModelForCreate(); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); validator - .Setup(validator => validator.ValidateAsync(payload, It.IsAny())) + .Setup(validator => validator.ValidateAsync(request, It.IsAny())) .ReturnsAsync( new ValidationResult( new List @@ -43,7 +43,7 @@ public async Task GivenPostAsync_WhenValidatorReturnsErrors_ThenResponseStatusCo var controller = new PlayerController(service.Object, logger.Object, validator.Object); // Act - var result = await controller.PostAsync(payload); + var result = await controller.PostAsync(request); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Never); @@ -56,10 +56,10 @@ public async Task GivenPostAsync_WhenValidatorReturnsErrors_ThenResponseStatusCo ), Times.Once ); - if (result is BadRequest response) + if (result is BadRequest httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status400BadRequest); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status400BadRequest); } } @@ -68,11 +68,12 @@ public async Task GivenPostAsync_WhenValidatorReturnsErrors_ThenResponseStatusCo public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenResponseStatusCodeShouldBe409Conflict() { // Arrange - var id = 1; - var player = PlayerFakes.CreateResponseModelForOneExistingById(id); - var payload = PlayerFakes.CreateRequestModelForOneExistingById(id); + var request = PlayerFakes.MakeRequestModelForCreate(); + var response = PlayerFakes.MakeResponseModelForCreate(); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); - service.Setup(service => service.RetrieveByIdAsync(It.IsAny())).ReturnsAsync(player); + service + .Setup(service => service.RetrieveByIdAsync(It.IsAny())) + .ReturnsAsync(response); validator .Setup(validator => validator.ValidateAsync( @@ -85,7 +86,7 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenR var controller = new PlayerController(service.Object, logger.Object, validator.Object); // Act - var result = await controller.PostAsync(payload); + var result = await controller.PostAsync(request); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); @@ -98,10 +99,10 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenR ), Times.Once ); - if (result is Conflict response) + if (result is Conflict httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status409Conflict); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status409Conflict); } } @@ -110,16 +111,14 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenR public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe201Created() { // Arrange - var id = 1; - var payload = PlayerFakes.CreateRequestModelForOneNew(); - var content = PlayerFakes.CreateResponseModelForOneNew(); + var id = 999; + var request = PlayerFakes.MakeRequestModelForCreate(); + var response = PlayerFakes.MakeResponseModelForCreate(); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveByIdAsync(id)) .ReturnsAsync(null as PlayerResponseModel); - service - .Setup(service => service.CreateAsync(It.IsAny())) - .ReturnsAsync(content); + service.Setup(service => service.CreateAsync(request)).ReturnsAsync(response); validator .Setup(validator => validator.ValidateAsync( @@ -135,7 +134,7 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenRes }; // Act - var result = await controller.PostAsync(payload); + var result = await controller.PostAsync(request); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); @@ -148,13 +147,13 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenRes ), Times.Once ); - if (result is CreatedAtRoute response) + if (result is CreatedAtRoute httpResult) { - response.Should().NotBeNull().And.BeOfType>(); - response.StatusCode.Should().Be(StatusCodes.Status201Created); - response.Value.Should().BeEquivalentTo(content); - response.RouteName.Should().Be("GetById"); - response.RouteValues.Should().NotBeNull().And.ContainKey("id"); + httpResult.Should().NotBeNull().And.BeOfType>(); + httpResult.StatusCode.Should().Be(StatusCodes.Status201Created); + httpResult.Value.Should().BeEquivalentTo(response); + httpResult.RouteName.Should().Be("GetById"); + httpResult.RouteValues.Should().NotBeNull().And.ContainKey("id"); } } @@ -167,9 +166,9 @@ public async Task GivenPostAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenRes public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsListOfPlayers_ThenResponseShouldBeEquivalentToListOfPlayers() { // Arrange - var players = PlayerFakes.CreateStarting11ResponseModels(); + var response = PlayerFakes.MakeResponseModelsForRetrieve(); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); - service.Setup(service => service.RetrieveAsync()).ReturnsAsync(players); + service.Setup(service => service.RetrieveAsync()).ReturnsAsync(response); var controller = new PlayerController(service.Object, logger.Object, validator.Object); @@ -178,12 +177,12 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsListOfPlayers_The // Assert service.Verify(service => service.RetrieveAsync(), Times.Once); - if (result is Ok> response) + if (result is Ok> httpResult) { - response.Should().NotBeNull().And.BeOfType>>(); - response.StatusCode.Should().Be(StatusCodes.Status200OK); - response.Value.Should().NotBeNull().And.BeOfType>(); - response.Value.Should().BeEquivalentTo(players); + httpResult.Should().NotBeNull().And.BeOfType>>(); + httpResult.StatusCode.Should().Be(StatusCodes.Status200OK); + httpResult.Value.Should().NotBeNull().And.BeOfType>(); + httpResult.Value.Should().BeEquivalentTo(response); } } @@ -202,10 +201,10 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsEmptyList_ThenRes // Assert service.Verify(service => service.RetrieveAsync(), Times.Once); - if (result is NotFound response) + if (result is NotFound httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status404NotFound); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound); } } @@ -214,22 +213,23 @@ public async Task GivenGetAsync_WhenServiceRetrieveAsyncReturnsEmptyList_ThenRes public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound() { // Arrange + var id = 999; var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service - .Setup(service => service.RetrieveByIdAsync(It.IsAny())) + .Setup(service => service.RetrieveByIdAsync(id)) .ReturnsAsync(null as PlayerResponseModel); var controller = new PlayerController(service.Object, logger.Object, validator.Object); // Act - var result = await controller.GetByIdAsync(It.IsAny()); + var result = await controller.GetByIdAsync(id); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); - if (result is NotFound response) + if (result is NotFound httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status404NotFound); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound); } } @@ -239,9 +239,9 @@ public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_Th { // Arrange var id = 1; - var player = PlayerFakes.CreateResponseModelForOneExistingById(id); + var response = PlayerFakes.MakeResponseModelForRetrieve(id); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); - service.Setup(service => service.RetrieveByIdAsync(id)).ReturnsAsync(player); + service.Setup(service => service.RetrieveByIdAsync(id)).ReturnsAsync(response); var controller = new PlayerController(service.Object, logger.Object, validator.Object); @@ -250,12 +250,12 @@ public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_Th // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); - if (result is Ok response) + if (result is Ok httpResult) { - response.Should().NotBeNull().And.BeOfType>(); - response.StatusCode.Should().Be(StatusCodes.Status200OK); - response.Value.Should().NotBeNull().And.BeOfType(); - response.Value.Should().BeEquivalentTo(player); + httpResult.Should().NotBeNull().And.BeOfType>(); + httpResult.StatusCode.Should().Be(StatusCodes.Status200OK); + httpResult.Value.Should().NotBeNull().And.BeOfType(); + httpResult.Value.Should().BeEquivalentTo(response); } } @@ -264,7 +264,7 @@ public async Task GivenGetByIdAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_Th public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound() { // Arrange - var squadNumber = 10; + var squadNumber = 999; var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveBySquadNumberAsync(squadNumber)) @@ -277,10 +277,10 @@ public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsy // Assert service.Verify(service => service.RetrieveBySquadNumberAsync(It.IsAny()), Times.Once); - if (result is NotFound response) + if (result is NotFound httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status404NotFound); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound); } } @@ -290,11 +290,11 @@ public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsy { // Arrange var squadNumber = 10; - var player = PlayerFakes.CreateResponseModelForOneExistingById(1); + var response = PlayerFakes.MakeResponseModelForRetrieve(1); var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveBySquadNumberAsync(squadNumber)) - .ReturnsAsync(player); + .ReturnsAsync(response); var controller = new PlayerController(service.Object, logger.Object, validator.Object); @@ -303,12 +303,12 @@ public async Task GivenGetBySquadNumberAsync_WhenServiceRetrieveBySquadNumberAsy // Assert service.Verify(service => service.RetrieveBySquadNumberAsync(It.IsAny()), Times.Once); - if (result is Ok response) + if (result is Ok httpResult) { - response.Should().NotBeNull().And.BeOfType>(); - response.StatusCode.Should().Be(StatusCodes.Status200OK); - response.Value.Should().NotBeNull().And.BeOfType(); - response.Value.Should().BeEquivalentTo(player); + httpResult.Should().NotBeNull().And.BeOfType>(); + httpResult.StatusCode.Should().Be(StatusCodes.Status200OK); + httpResult.Value.Should().NotBeNull().And.BeOfType(); + httpResult.Value.Should().BeEquivalentTo(response); } } @@ -322,14 +322,14 @@ public async Task GivenPutAsync_WhenValidatorReturnsErrors_ThenResponseStatusCod { // Arrange var id = 1; - var player = PlayerFakes.CreateRequestModelForOneExistingById(id); - player.SquadNumber = -99; // Invalid Squad Number + var request = PlayerFakes.MakeRequestModelForUpdate(id); + request.SquadNumber = -99; // Invalid Squad Number var (service, logger, validator) = PlayerMocks.InitControllerMocks(); var controller = new PlayerController(service.Object, logger.Object, validator.Object); validator - .Setup(v => v.ValidateAsync(player, It.IsAny())) + .Setup(validator => validator.ValidateAsync(request, It.IsAny())) .ReturnsAsync( new ValidationResult( new List @@ -340,7 +340,7 @@ public async Task GivenPutAsync_WhenValidatorReturnsErrors_ThenResponseStatusCod ); // Act - var result = await controller.PutAsync(id, player); + var result = await controller.PutAsync(id, request); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Never); @@ -353,10 +353,10 @@ public async Task GivenPutAsync_WhenValidatorReturnsErrors_ThenResponseStatusCod ), Times.Once ); - if (result is BadRequest response) + if (result is BadRequest httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status400BadRequest); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status400BadRequest); } } @@ -365,7 +365,7 @@ public async Task GivenPutAsync_WhenValidatorReturnsErrors_ThenResponseStatusCod public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound() { // Arrange - var id = 1; + var id = 999; var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveByIdAsync(id)) @@ -395,10 +395,10 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResp ), Times.Once ); - if (result is NotFound response) + if (result is NotFound httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status404NotFound); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound); } } @@ -408,14 +408,13 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenRe { // Arrange var id = 1; - var player = PlayerFakes.CreateRequestModelForOneExistingById(id); - player.FirstName = "Emiliano"; - player.MiddleName = string.Empty; + var request = PlayerFakes.MakeRequestModelForUpdate(id); + var response = PlayerFakes.MakeResponseModelForUpdate(id); + request.FirstName = "Emiliano"; + request.MiddleName = string.Empty; var (service, logger, validator) = PlayerMocks.InitControllerMocks(); - service - .Setup(service => service.RetrieveByIdAsync(id)) - .ReturnsAsync(PlayerFakes.CreateResponseModelForOneExistingById(id)); - service.Setup(service => service.UpdateAsync(It.IsAny())); + service.Setup(service => service.RetrieveByIdAsync(id)).ReturnsAsync(response); + service.Setup(service => service.UpdateAsync(request)); validator .Setup(validator => validator.ValidateAsync( @@ -428,15 +427,15 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenRe var controller = new PlayerController(service.Object, logger.Object, validator.Object); // Act - var result = await controller.PutAsync(id, player); + var result = await controller.PutAsync(id, request); // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); service.Verify(service => service.UpdateAsync(It.IsAny()), Times.Once); - if (result is NoContent response) + if (result is NoContent httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status204NoContent); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status204NoContent); } } @@ -449,7 +448,7 @@ public async Task GivenPutAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_ThenRe public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenResponseStatusCodeShouldBe404NotFound() { // Arrange - var id = 2; + var id = 999; var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveByIdAsync(id)) @@ -463,10 +462,10 @@ public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsNull_ThenR // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); service.Verify(service => service.DeleteAsync(It.IsAny()), Times.Never); - if (result is NotFound response) + if (result is NotFound httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status404NotFound); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status404NotFound); } } @@ -479,7 +478,7 @@ public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_The var (service, logger, validator) = PlayerMocks.InitControllerMocks(); service .Setup(service => service.RetrieveByIdAsync(id)) - .ReturnsAsync(PlayerFakes.CreateResponseModelForOneExistingById(id)); + .ReturnsAsync(PlayerFakes.MakeResponseModelForUpdate(id)); service.Setup(service => service.DeleteAsync(id)); var controller = new PlayerController(service.Object, logger.Object, validator.Object); @@ -490,10 +489,10 @@ public async Task GivenDeleteAsync_WhenServiceRetrieveByIdAsyncReturnsPlayer_The // Assert service.Verify(service => service.RetrieveByIdAsync(It.IsAny()), Times.Once); service.Verify(service => service.DeleteAsync(It.IsAny()), Times.Once); - if (result is NoContent response) + if (result is NoContent httpResult) { - response.Should().NotBeNull().And.BeOfType(); - response.StatusCode.Should().Be(StatusCodes.Status204NoContent); + httpResult.Should().NotBeNull().And.BeOfType(); + httpResult.StatusCode.Should().Be(StatusCodes.Status204NoContent); } } diff --git a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs index a841e30..b31ec9e 100644 --- a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs +++ b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs @@ -25,11 +25,10 @@ public PlayerServiceTests() public async Task GivenCreateAsync_WhenRepositoryAddAsync_ThenAddsPlayerToRepositoryAndRemovesCache() { // Arrange - var response = PlayerFakes.CreateResponseModelForOneExistingById(9); + var request = PlayerFakes.MakeRequestModelForCreate(); + var response = PlayerFakes.MakeResponseModelForCreate(); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); - mapper - .Setup(mapper => mapper.Map(It.IsAny())) - .Returns(response); + mapper.Setup(mapper => mapper.Map(request)).Returns(response); var service = new PlayerService( repository.Object, @@ -39,7 +38,7 @@ public async Task GivenCreateAsync_WhenRepositoryAddAsync_ThenAddsPlayerToReposi ); // Act - await service.CreateAsync(It.IsAny()); + await service.CreateAsync(request); // Assert repository.Verify(repository => repository.AddAsync(It.IsAny()), Times.Once); @@ -59,14 +58,12 @@ public async Task GivenCreateAsync_WhenRepositoryAddAsync_ThenAddsPlayerToReposi public async Task GivenRetrieveAsync_WhenRepositoryGetAllAsyncReturnsPlayers_ThenCacheCreateEntryAndResultShouldBeListOfPlayers() { // Arrange - var players = PlayerFakes.GetStarting11(); - var response = PlayerFakes.CreateStarting11ResponseModels(); + var value = It.IsAny(); + var players = PlayerFakes.MakeStarting11(); + var response = PlayerFakes.MakeResponseModelsForRetrieve(); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); repository.Setup(repository => repository.GetAllAsync()).ReturnsAsync(players); - mapper - .Setup(mapper => mapper.Map>(It.IsAny>())) - .Returns(response); - var value = It.IsAny(); + mapper.Setup(mapper => mapper.Map>(players)).Returns(response); var service = new PlayerService( repository.Object, @@ -94,14 +91,12 @@ public async Task GivenRetrieveAsync_WhenRepositoryGetAllAsyncReturnsPlayers_The public async Task GivenRetrieveAsync_WhenExecutedForTheSecondTime_ThenSecondExecutionTimeShouldBeLessThanFirst() { // Arrange - var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); - repository - .Setup(repository => repository.GetAllAsync()) - .ReturnsAsync(PlayerFakes.GetStarting11()); - mapper - .Setup(mapper => mapper.Map>(It.IsAny>())) - .Returns(PlayerFakes.CreateStarting11ResponseModels()); var value = It.IsAny(); + var players = PlayerFakes.MakeStarting11(); + var response = PlayerFakes.MakeResponseModelsForRetrieve(); + var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); + repository.Setup(repository => repository.GetAllAsync()).ReturnsAsync(players); + mapper.Setup(mapper => mapper.Map>(players)).Returns(response); var service = new PlayerService( repository.Object, @@ -133,10 +128,9 @@ public async Task GivenRetrieveAsync_WhenExecutedForTheSecondTime_ThenSecondExec public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsNull_TheResultShouldBeNull() { // Arrange + var id = 999; var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); - repository - .Setup(repository => repository.FindByIdAsync(It.IsAny())) - .ReturnsAsync((Player?)null); + repository.Setup(repository => repository.FindByIdAsync(id)).ReturnsAsync(null as Player); var service = new PlayerService( repository.Object, @@ -146,7 +140,7 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsNull_ ); // Act - var result = await service.RetrieveByIdAsync(It.IsAny()); + var result = await service.RetrieveByIdAsync(id); // Assert repository.Verify(repository => repository.FindByIdAsync(It.IsAny()), Times.Once); @@ -159,16 +153,12 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsNull_ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_TheResultShouldBePlayer() { // Arrange - var id = 9; - var player = PlayerFakes.GetOneExistingById(id); - var response = PlayerFakes.CreateResponseModelForOneExistingById(id); + var id = 1; + var player = PlayerFakes.MakeFromStarting11ById(id); + var response = PlayerFakes.MakeResponseModelForRetrieve(id); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); - repository - .Setup(repository => repository.FindByIdAsync(It.IsAny())) - .ReturnsAsync(player); - mapper - .Setup(mapper => mapper.Map(It.IsAny())) - .Returns(response); + repository.Setup(repository => repository.FindByIdAsync(id)).ReturnsAsync(player); + mapper.Setup(mapper => mapper.Map(player)).Returns(response); var service = new PlayerService( repository.Object, @@ -178,7 +168,7 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsPlaye ); // Act - var result = await service.RetrieveByIdAsync(It.IsAny()); + var result = await service.RetrieveByIdAsync(id); // Assert repository.Verify(repository => repository.FindByIdAsync(It.IsAny()), Times.Once); @@ -192,10 +182,11 @@ public async Task GivenRetrieveByIdAsync_WhenRepositoryFindByIdAsyncReturnsPlaye public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumberAsyncReturnsNull_ThenResultShouldBeNull() { // Arrange + var squadNumber = 999; var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); repository - .Setup(repository => repository.FindBySquadNumberAsync(It.IsAny())) - .ReturnsAsync((Player?)null); + .Setup(repository => repository.FindBySquadNumberAsync(squadNumber)) + .ReturnsAsync(null as Player); var service = new PlayerService( repository.Object, @@ -205,7 +196,7 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe ); // Act - var result = await service.RetrieveBySquadNumberAsync(It.IsAny()); + var result = await service.RetrieveBySquadNumberAsync(squadNumber); // Assert repository.Verify( @@ -221,16 +212,15 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumberAsyncReturnsPlayer_ThenResultShouldBePlayer() { // Arrange - var id = 9; - var player = PlayerFakes.GetOneExistingById(id); - var response = PlayerFakes.CreateResponseModelForOneExistingById(id); + var id = 1; + var player = PlayerFakes.MakeFromStarting11ById(id); + var squadNumber = player.SquadNumber; + var response = PlayerFakes.MakeResponseModelForRetrieve(id); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); repository - .Setup(repository => repository.FindBySquadNumberAsync(It.IsAny())) + .Setup(repository => repository.FindBySquadNumberAsync(squadNumber)) .ReturnsAsync(player); - mapper - .Setup(mapper => mapper.Map(It.IsAny())) - .Returns(response); + mapper.Setup(mapper => mapper.Map(player)).Returns(response); var service = new PlayerService( repository.Object, @@ -240,7 +230,7 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe ); // Act - var result = await service.RetrieveBySquadNumberAsync(It.IsAny()); + var result = await service.RetrieveBySquadNumberAsync(squadNumber); // Assert repository.Verify( @@ -261,13 +251,11 @@ public async Task GivenRetrieveBySquadNumberAsync_WhenRepositoryFindBySquadNumbe public async Task GivenUpdateAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_ThenRepositoryUpdateAsyncAndCacheRemove() { // Arrange - var id = 9; - var player = PlayerFakes.GetOneExistingById(id); - var request = PlayerFakes.CreateRequestModelForOneExistingById(id); + var id = 1; + var player = PlayerFakes.MakeFromStarting11ById(id); + var request = PlayerFakes.MakeRequestModelForUpdate(id); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); - repository - .Setup(repository => repository.FindByIdAsync(It.IsAny())) - .ReturnsAsync(player); + repository.Setup(repository => repository.FindByIdAsync(id)).ReturnsAsync(player); var service = new PlayerService( repository.Object, @@ -298,8 +286,8 @@ public async Task GivenUpdateAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_Then public async Task GivenDeleteAsync_WhenRepositoryFindByIdAsyncReturnsPlayer_ThenRepositoryDeleteAsyncAndCacheRemove() { // Arrange - var id = 9; - var player = PlayerFakes.GetOneExistingById(id); + var id = 2; + var player = PlayerFakes.MakeFromStarting11ById(id); var (repository, logger, memoryCache, mapper) = PlayerMocks.InitServiceMocks(); repository .Setup(repository => repository.FindByIdAsync(It.IsAny())) diff --git a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs index 4ac0ea8..470b27e 100644 --- a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs +++ b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs @@ -48,7 +48,7 @@ CREATE TABLE players ( public static void Seed(this PlayerDbContext context) { - context.Players.AddRange(PlayerFakes.GetStarting11()); + context.Players.AddRange(PlayerFakes.MakeStarting11()); context.SaveChanges(); } } diff --git a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs index 0adf0bb..b856a5e 100644 --- a/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs +++ b/test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs @@ -12,7 +12,7 @@ namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities; /// public static class PlayerFakes { - public static List GetStarting11() + public static List MakeStarting11() { return [ @@ -170,24 +170,16 @@ public static List GetStarting11() ]; } - public static List CreateStarting11ResponseModels() => - [ - .. GetStarting11() - .Select(player => new PlayerResponseModel - { - Id = player.Id, - FullName = - $"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(), - Birth = $"{player.DateOfBirth:MMMM d, yyyy}", - Dorsal = player.SquadNumber, - Position = player.Position, - Club = player.Team, - League = player.League, - Starting11 = player.Starting11 ? "Yes" : "No" - }) - ]; + public static Player MakeFromStarting11ById(long id) + { + var player = + MakeStarting11().SingleOrDefault(player => player.Id == id) + ?? throw new ArgumentNullException($"Player with ID {id} not found."); + + return player; + } - public static Player CreateOneNew() + public static Player MakeNew() { return new() { @@ -205,11 +197,15 @@ public static Player CreateOneNew() }; } - public static PlayerRequestModel CreateRequestModelForOneNew() + /* ------------------------------------------------------------------------- + * Create + * ---------------------------------------------------------------------- */ + + public static PlayerRequestModel MakeRequestModelForCreate() { - var player = CreateOneNew(); + var player = MakeNew(); - return new() + return new PlayerRequestModel() { Id = player.Id, FirstName = player.FirstName, @@ -223,9 +219,9 @@ public static PlayerRequestModel CreateRequestModelForOneNew() }; } - public static PlayerResponseModel CreateResponseModelForOneNew() + public static PlayerResponseModel MakeResponseModelForCreate() { - var player = CreateOneNew(); + var player = MakeNew(); return new PlayerResponseModel { @@ -241,19 +237,14 @@ public static PlayerResponseModel CreateResponseModelForOneNew() }; } - public static Player GetOneExistingById(long id) - { - var player = - GetStarting11().SingleOrDefault(player => player.Id == id) - ?? throw new ArgumentNullException($"Player with ID {id} not found."); - - return player; - } + /* ------------------------------------------------------------------------- + * Retrieve + * ---------------------------------------------------------------------- */ - public static PlayerRequestModel CreateRequestModelForOneExistingById(long id) + public static PlayerRequestModel MakeRequestModelForRetrieve(long id) { var player = - GetStarting11().SingleOrDefault(player => player.Id == id) + MakeStarting11().SingleOrDefault(player => player.Id == id) ?? throw new ArgumentNullException($"Player with ID {id} not found."); return new PlayerRequestModel @@ -270,10 +261,10 @@ public static PlayerRequestModel CreateRequestModelForOneExistingById(long id) }; } - public static PlayerResponseModel CreateResponseModelForOneExistingById(long id) + public static PlayerResponseModel MakeResponseModelForRetrieve(long id) { var player = - GetStarting11().SingleOrDefault(player => player.Id == id) + MakeStarting11().SingleOrDefault(player => player.Id == id) ?? throw new ArgumentNullException($"Player with ID {id} not found."); return new PlayerResponseModel @@ -289,4 +280,35 @@ public static PlayerResponseModel CreateResponseModelForOneExistingById(long id) Starting11 = player.Starting11 ? "Yes" : "No" }; } + + public static List MakeResponseModelsForRetrieve() => + [ + .. MakeStarting11() + .Select(player => new PlayerResponseModel + { + Id = player.Id, + FullName = + $"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(), + Birth = $"{player.DateOfBirth:MMMM d, yyyy}", + Dorsal = player.SquadNumber, + Position = player.Position, + Club = player.Team, + League = player.League, + Starting11 = player.Starting11 ? "Yes" : "No" + }) + ]; + + /* ------------------------------------------------------------------------- + * Update + * ---------------------------------------------------------------------- */ + + public static PlayerRequestModel MakeRequestModelForUpdate(long id) + { + return MakeRequestModelForRetrieve(id); + } + + public static PlayerResponseModel MakeResponseModelForUpdate(long id) + { + return MakeResponseModelForRetrieve(id); + } }