From 1ceed5f09004f934670a84bdc1b234e7c7402be5 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sun, 12 Feb 2023 20:20:55 +0100 Subject: [PATCH] :white_check_mark: Implement UTs on BarsController --- Tests/Controllers/BarsControllerTest.cs | 148 +++ Tests/Controllers/CustomersControllerTest.cs | 1 - cat_cafe/Controllers/BarsController.cs | 2 +- coverage.xml | 1202 ------------------ 4 files changed, 149 insertions(+), 1204 deletions(-) create mode 100644 Tests/Controllers/BarsControllerTest.cs delete mode 100644 coverage.xml diff --git a/Tests/Controllers/BarsControllerTest.cs b/Tests/Controllers/BarsControllerTest.cs new file mode 100644 index 0000000..9b71c8a --- /dev/null +++ b/Tests/Controllers/BarsControllerTest.cs @@ -0,0 +1,148 @@ +using AutoMapper; +using cat_cafe.Dto; +using cat_cafe.Entities; +using cat_cafe.Mappers; +using cat_cafe.Repositories; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace cat_cafe.Controllers.Tests +{ + + [TestClass()] + public class BarsControllerTest + { + + private readonly ILogger logger = new NullLogger(); + + private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(BarMapper))); + + private readonly DbContextOptions options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "CatCafeTests") + .Options; + + private readonly Bar bleep = new() + { + Id = 1, + Name = "bleep" + }; + + private readonly Bar bloop = new() + { + Id = 2, + Name= "bloop" + }; + + + private readonly BarDto bleepDto; + + private readonly BarDto bloopDto; + + private readonly IMapper mapper; + + private readonly CatCafeContext context; + + private readonly BarsController controller; + + public BarsControllerTest() + { + mapper = mapperConf.CreateMapper(); + context = new CatCafeContext(options); + controller = new BarsController(context, mapper, logger); + bleepDto = mapper.Map(bleep); + bloopDto = mapper.Map(bloop); + } + + + [TestInitialize] + public void BeforeEach() + { + context.Database.EnsureCreated(); + context.Bars.AddRange(bleep, bloop); + context.SaveChanges(); + } + + [TestCleanup] + public void AfterEach() + { + context.Database.EnsureDeleted(); + } + + [TestMethod()] + public async Task GetBarsTest() + { + // control response type + var actual = await controller.GetBars(); + actual.Result.Should().BeOfType(); + + // control response object + var actualResult = actual.Result as OkObjectResult; + actualResult.Should().NotBeNull(); + actualResult!.Value.Should() + .BeEquivalentTo(new List() { bleepDto, bloopDto }.AsEnumerable()); + } + + [TestMethod()] + public async Task GetBarTest() + { + // control response type + var actual = await controller.GetBar(1); + actual.Result.Should().BeOfType(); + + // control response object + var actualResult = actual.Result as OkObjectResult; + actualResult.Should().NotBeNull(); + actualResult!.Value.Should().BeEquivalentTo(bleepDto); + } + + + [TestMethod()] + public async Task PutBarTest() + { + // Arrange + BarDto blarp = new() { Id = 2, Name = "blarp" }; + + // Act + var responseType = await controller.PutBar(bloop.Id, blarp); + + // Assert + responseType.Should().BeOfType(); + var actual = await controller.GetBar(bloop.Id); + var actualResult = actual.Result as OkObjectResult; + actualResult!.Value.Should().BeEquivalentTo(blarp); + } + + + [TestMethod()] + public async Task PostBarTest() + { + // Arrange + BarDto brrrrr = new() { Id = 3, Name = "brrrrr" }; + + // Act + var responseType = await controller.PostBar(brrrrr); + + // Assert + responseType.Result.Should().BeOfType(); + var actual = await controller.GetBar(brrrrr.Id); + var actualResult = actual.Result as OkObjectResult; + actualResult!.Value.Should().BeEquivalentTo(brrrrr); + } + + [TestMethod()] + public async Task DeleteBarTest() + { + // Act + var responseType = await controller.DeleteBar(bleep.Id); + + // Assert + responseType.Should().BeOfType(); + + var actual = await controller.GetBar(bleep.Id); + actual.Result.Should().BeOfType(); + } + } +} \ No newline at end of file diff --git a/Tests/Controllers/CustomersControllerTest.cs b/Tests/Controllers/CustomersControllerTest.cs index 2a29ee6..ed82737 100644 --- a/Tests/Controllers/CustomersControllerTest.cs +++ b/Tests/Controllers/CustomersControllerTest.cs @@ -3,7 +3,6 @@ using cat_cafe.Dto; using cat_cafe.Entities; using cat_cafe.Mappers; using cat_cafe.Repositories; -using cat_cafe.WeSo; using FluentAssertions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; diff --git a/cat_cafe/Controllers/BarsController.cs b/cat_cafe/Controllers/BarsController.cs index 8cccc28..376557b 100644 --- a/cat_cafe/Controllers/BarsController.cs +++ b/cat_cafe/Controllers/BarsController.cs @@ -113,7 +113,7 @@ namespace cat_cafe.Controllers // POST: api/v1/Bars // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] - public async Task> CreateBar(BarDto barDto) + public async Task> PostBar(BarDto barDto) { try { diff --git a/coverage.xml b/coverage.xml deleted file mode 100644 index 7e150cb..0000000 --- a/coverage.xml +++ /dev/null @@ -1,1202 +0,0 @@ - - - - - - cat_cafe.dll - 2023-02-12T07:28:50 - cat_cafe - - - - - - - - - - - - - - - - - - - - - Program - - - - - System.Void Program::<Main>$(System.String[]) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Program/<>c__DisplayClass0_0/<<<Main>$>b__5>d - - - - - cat_cafe.WeSo.WebSocketHandler - - - - - System.Void cat_cafe.WeSo.WebSocketHandler::.ctor(System.Collections.Generic.List`1<System.Net.WebSockets.WebSocket>) - - - - - - - - - - - - - - - cat_cafe.WeSo.WebSocketHandler/<BroadcastMessageAsync>d__2 - - - - - System.Void cat_cafe.WeSo.WebSocketHandler/<BroadcastMessageAsync>d__2::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Repositories.CatCafeContext - - - - - Microsoft.EntityFrameworkCore.DbSet`1<cat_cafe.Entities.Cat> cat_cafe.Repositories.CatCafeContext::get_Cats() - - - - - - - - - - - Microsoft.EntityFrameworkCore.DbSet`1<cat_cafe.Entities.Bar> cat_cafe.Repositories.CatCafeContext::get_Bars() - - - - - - - - - - - Microsoft.EntityFrameworkCore.DbSet`1<cat_cafe.Entities.Customer> cat_cafe.Repositories.CatCafeContext::get_Customers() - - - - - - - - - - - System.Void cat_cafe.Repositories.CatCafeContext::OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder) - - - - - - - - - - - - - - - - System.Void cat_cafe.Repositories.CatCafeContext::.ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1<cat_cafe.Repositories.CatCafeContext>) - - - - - - - - - - - - - - cat_cafe.Mappers.BarMapper - - - - - System.Void cat_cafe.Mappers.BarMapper::.ctor() - - - - - - - - - - - - - - - - - cat_cafe.Mappers.CatMapper - - - - - System.Void cat_cafe.Mappers.CatMapper::.ctor() - - - - - - - - - - - - - - - cat_cafe.Mappers.CustomerMapper - - - - - System.Void cat_cafe.Mappers.CustomerMapper::.ctor() - - - - - - - - - - - - - - - cat_cafe.Entities.Bar - - - - - System.Int64 cat_cafe.Entities.Bar::get_Id() - - - - - - - - - - - System.String cat_cafe.Entities.Bar::get_Name() - - - - - - - - - - - System.Collections.Generic.List`1<cat_cafe.Entities.Cat> cat_cafe.Entities.Bar::get_Cats() - - - - - - - - - - - System.Void cat_cafe.Entities.Bar::AddCat(cat_cafe.Entities.Cat) - - - - - - - - - - - - - System.Void cat_cafe.Entities.Bar::RemoveCat(cat_cafe.Entities.Cat) - - - - - - - - - - - - - - cat_cafe.Entities.Cat - - - - - System.Int64 cat_cafe.Entities.Cat::get_Id() - - - - - - - - - - - System.String cat_cafe.Entities.Cat::get_Name() - - - - - - - - - - - System.Int32 cat_cafe.Entities.Cat::get_Age() - - - - - - - - - - - System.Nullable`1<System.Int64> cat_cafe.Entities.Cat::get_BarId() - - - - - - - - - - - cat_cafe.Entities.Bar cat_cafe.Entities.Cat::get_Bar() - - - - - - - - - - - System.String cat_cafe.Entities.Cat::get_Meow() - - - - - - - - - - - - cat_cafe.Entities.Customer - - - - - System.Int64 cat_cafe.Entities.Customer::get_Id() - - - - - - - - - - - System.String cat_cafe.Entities.Customer::get_FullName() - - - - - - - - - - - System.Int32 cat_cafe.Entities.Customer::get_Age() - - - - - - - - - - - - cat_cafe.Dto.BarDto - - - - - System.Int64 cat_cafe.Dto.BarDto::get_Id() - - - - - - - - - - - System.String cat_cafe.Dto.BarDto::get_Name() - - - - - - - - - - - System.Collections.Generic.List`1<System.Int64> cat_cafe.Dto.BarDto::get_CatIds() - - - - - - - - - - - - cat_cafe.Dto.CatDto - - - - - System.Int64 cat_cafe.Dto.CatDto::get_Id() - - - - - - - - - - - System.String cat_cafe.Dto.CatDto::get_Name() - - - - - - - - - - - System.Nullable`1<System.Int64> cat_cafe.Dto.CatDto::get_BarId() - - - - - - - - - - - - cat_cafe.Dto.CustomerDto - - - - - System.Int64 cat_cafe.Dto.CustomerDto::get_Id() - - - - - - - - - - - System.String cat_cafe.Dto.CustomerDto::get_FullName() - - - - - - - - - - - System.Int32 cat_cafe.Dto.CustomerDto::get_Age() - - - - - - - - - - - - cat_cafe.Controllers.BarsController - - - - - System.Void cat_cafe.Controllers.BarsController::.ctor(cat_cafe.Repositories.CatCafeContext,AutoMapper.IMapper,Microsoft.Extensions.Logging.ILogger`1<cat_cafe.Controllers.BarsController>) - - - - - - - - - - - - - - - - - cat_cafe.Controllers.BarsController/<CreateBar>d__7 - - - - - System.Void cat_cafe.Controllers.BarsController/<CreateBar>d__7::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.BarsController/<DeleteBar>d__8 - - - - - System.Void cat_cafe.Controllers.BarsController/<DeleteBar>d__8::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.BarsController/<GetBar>d__5 - - - - - System.Void cat_cafe.Controllers.BarsController/<GetBar>d__5::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.BarsController/<GetBars>d__4 - - - - - System.Void cat_cafe.Controllers.BarsController/<GetBars>d__4::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.BarsController/<PutBar>d__6 - - - - - System.Void cat_cafe.Controllers.BarsController/<PutBar>d__6::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController - - - - - System.Void cat_cafe.Controllers.CatsController::.ctor(cat_cafe.Repositories.CatCafeContext,AutoMapper.IMapper,Microsoft.Extensions.Logging.ILogger`1<cat_cafe.Controllers.CatsController>,cat_cafe.WeSo.WebSocketHandler) - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<DeleteCat>d__10 - - - - - System.Void cat_cafe.Controllers.CatsController/<DeleteCat>d__10::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<GetCat>d__7 - - - - - System.Void cat_cafe.Controllers.CatsController/<GetCat>d__7::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<GetCats>d__5 - - - - - System.Void cat_cafe.Controllers.CatsController/<GetCats>d__5::MoveNext() - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<GetCatsV2>d__6 - - - - - System.Void cat_cafe.Controllers.CatsController/<GetCatsV2>d__6::MoveNext() - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<PostCat>d__9 - - - - - System.Void cat_cafe.Controllers.CatsController/<PostCat>d__9::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CatsController/<PutCat>d__8 - - - - - System.Void cat_cafe.Controllers.CatsController/<PutCat>d__8::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController - - - - - System.Void cat_cafe.Controllers.CustomersController::.ctor(cat_cafe.Repositories.CatCafeContext,AutoMapper.IMapper,Microsoft.Extensions.Logging.ILogger`1<cat_cafe.Controllers.CustomersController>) - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController/<DeleteCustomer>d__8 - - - - - System.Void cat_cafe.Controllers.CustomersController/<DeleteCustomer>d__8::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController/<GetCustomer>d__5 - - - - - System.Void cat_cafe.Controllers.CustomersController/<GetCustomer>d__5::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController/<GetCustomers>d__4 - - - - - System.Void cat_cafe.Controllers.CustomersController/<GetCustomers>d__4::MoveNext() - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController/<PostCustomer>d__7 - - - - - System.Void cat_cafe.Controllers.CustomersController/<PostCustomer>d__7::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - cat_cafe.Controllers.CustomersController/<PutCustomer>d__6 - - - - - System.Void cat_cafe.Controllers.CustomersController/<PutCustomer>d__6::MoveNext() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file