From 4346c43ec1f132e6dde61e88decd2c296dc07dc9 Mon Sep 17 00:00:00 2001 From: Fiyaz Bin Hasan Date: Sat, 8 Apr 2023 14:30:38 +0600 Subject: [PATCH] TypedResults update --- .../UnitTests/TodoInMemoryTests.cs | 61 ++++++++++------ .../UnitTests/TodoMoqTests.cs | 69 ++++++++++++------- .../WebMinRouteGroup/TodoEndpointsV1.cs | 7 +- .../WebMinRouteGroup/TodoEndpointsV2.cs | 7 +- 4 files changed, 93 insertions(+), 51 deletions(-) diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs index b74a5d7..67268e9 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs @@ -15,10 +15,14 @@ public class TodoInMemoryTests await using var context = new MockDb().CreateDbContext(); // Act - var notFoundResult = (NotFound)await TodoEndpointsV1.GetTodo(1, context); + var result = await TodoEndpointsV1.GetTodo(1, context); //Assert - Assert.Equal(404, notFoundResult.StatusCode); + Assert.IsType, NotFound>>(result); + + var notFoundResult = (NotFound) result.Result; + + Assert.NotNull(notFoundResult); } // @@ -47,22 +51,24 @@ public class TodoInMemoryTests await context.SaveChangesAsync(); // Act - var okResult = (Ok>)await TodoEndpointsV1.GetAllTodos(context); + var result = await TodoEndpointsV1.GetAllTodos(context); //Assert - Assert.Equal(200, okResult.StatusCode); - var foundTodos = Assert.IsAssignableFrom>(okResult.Value); + Assert.IsType>>(result); - Assert.NotEmpty(foundTodos); - Assert.Collection(foundTodos, todo1 => + var okResult = (Ok>)result; + + Assert.NotNull(okResult.Value); + Assert.NotEmpty(okResult.Value); + Assert.Collection(okResult.Value, todo1 => { + Assert.Equal(1, todo1.Id); Assert.Equal("Test title 1", todo1.Title); - Assert.Equal("Test description 1", todo1.Description); Assert.False(todo1.IsDone); }, todo2 => { + Assert.Equal(2, todo2.Id); Assert.Equal("Test title 2", todo2.Title); - Assert.Equal("Test description 2", todo2.Description); Assert.True(todo2.IsDone); }); } @@ -85,12 +91,15 @@ public class TodoInMemoryTests await context.SaveChangesAsync(); // Act - var okResult = (Ok)await TodoEndpointsV1.GetTodo(1, context); + var result = await TodoEndpointsV1.GetTodo(1, context); //Assert - Assert.Equal(200, okResult.StatusCode); - var foundTodo = Assert.IsAssignableFrom(okResult.Value); - Assert.Equal(1, foundTodo.Id); + Assert.IsType, NotFound>>(result); + + var okResult = (Ok)result.Result; + + Assert.NotNull(okResult.Value); + Assert.Equal(1, okResult.Value.Id); } // @@ -109,12 +118,15 @@ public class TodoInMemoryTests }; //Act - var createdResult = (Created)await TodoEndpointsV1.CreateTodo(newTodo, context); + var result = await TodoEndpointsV1.CreateTodo(newTodo, context); //Assert - Assert.Equal(201, createdResult.StatusCode); + Assert.IsType>(result); + + var createdResult = (Created) result; + + Assert.NotNull(createdResult); Assert.NotNull(createdResult.Location); - Assert.IsAssignableFrom(createdResult.Value); Assert.NotEmpty(context.Todos); Assert.Collection(context.Todos, todo => @@ -149,12 +161,15 @@ public class TodoInMemoryTests }; //Act - var createdResult = (Created)await TodoEndpointsV1.UpdateTodo(updatedTodo, context); + var result = await TodoEndpointsV1.UpdateTodo(updatedTodo, context); //Assert - Assert.Equal(201, createdResult.StatusCode); + Assert.IsType, NotFound>>(result); + + var createdResult = (Created)result.Result; + + Assert.NotNull(createdResult); Assert.NotNull(createdResult.Location); - Assert.IsAssignableFrom(createdResult.Value); var todoInDb = await context.Todos.FindAsync(1); @@ -181,10 +196,14 @@ public class TodoInMemoryTests await context.SaveChangesAsync(); //Act - var noContentResult = (NoContent)await TodoEndpointsV1.DeleteTodo(existingTodo.Id, context); + var result = await TodoEndpointsV1.DeleteTodo(existingTodo.Id, context); //Assert - Assert.Equal(204, noContentResult.StatusCode); + Assert.IsType>(result); + + var noContentResult = (NoContent)result.Result; + + Assert.NotNull(noContentResult); Assert.Empty(context.Todos); } } diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoMoqTests.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoMoqTests.cs index 613a223..fa88bba 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoMoqTests.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoMoqTests.cs @@ -18,10 +18,14 @@ public class TodoMoqTests .ReturnsAsync((Todo?)null); // Act - var notFoundResult = (NotFound)await TodoEndpointsV2.GetTodo(1, mock.Object); + var result = await TodoEndpointsV2.GetTodo(1, mock.Object); //Assert - Assert.Equal(404, notFoundResult.StatusCode); + Assert.IsType, NotFound>>(result); + + var notFoundResult = (NotFound) result.Result; + + Assert.NotNull(notFoundResult); } [Fact] @@ -47,14 +51,16 @@ public class TodoMoqTests }); // Act - var okResult = (Ok>)await TodoEndpointsV2.GetAllTodos(mock.Object); + var result = await TodoEndpointsV2.GetAllTodos(mock.Object); //Assert - Assert.Equal(200, okResult.StatusCode); - var foundTodos = Assert.IsAssignableFrom>(okResult.Value); + Assert.IsType>>(result); - Assert.NotEmpty(foundTodos); - Assert.Collection(foundTodos, todo1 => + var okResult = (Ok>) result; + + Assert.NotNull(okResult.Value); + Assert.NotEmpty(okResult.Value); + Assert.Collection(okResult.Value, todo1 => { Assert.Equal(1, todo1.Id); Assert.Equal("Test title 1", todo1.Title); @@ -90,14 +96,16 @@ public class TodoMoqTests }); // Act - var okResult = (Ok>)await TodoEndpointsV2.GetAllIncompletedTodos(mock.Object); + var result = await TodoEndpointsV2.GetAllIncompletedTodos(mock.Object); //Assert - Assert.Equal(200, okResult.StatusCode); - var foundTodos = Assert.IsAssignableFrom>(okResult.Value); + Assert.IsType>>(result); - Assert.NotEmpty(foundTodos); - Assert.Collection(foundTodos, todo1 => + var okResult = (Ok>) result; + + Assert.NotNull(okResult.Value); + Assert.NotEmpty(okResult.Value); + Assert.Collection(okResult.Value, todo1 => { Assert.Equal(1, todo1.Id); Assert.Equal("Test title 1", todo1.Title); @@ -125,12 +133,15 @@ public class TodoMoqTests }); // Act - var okResult = (Ok)await TodoEndpointsV2.GetTodo(1, mock.Object); + var result = await TodoEndpointsV2.GetTodo(1, mock.Object); //Assert - Assert.Equal(200, okResult.StatusCode); - var foundTodo = Assert.IsAssignableFrom(okResult.Value); - Assert.Equal(1, foundTodo.Id); + Assert.IsType, NotFound>>(result); + + var okResult = (Ok) result.Result; + + Assert.NotNull(okResult.Value); + Assert.Equal(1, okResult.Value.Id); } [Fact] @@ -153,12 +164,15 @@ public class TodoMoqTests .Returns(Task.CompletedTask); //Act - var createdResult = (Created)await TodoEndpointsV2.CreateTodo(newTodo, mock.Object); + var result = await TodoEndpointsV2.CreateTodo(newTodo, mock.Object); //Assert - Assert.Equal(201, createdResult.StatusCode); + Assert.IsType>(result); + + var createdResult = (Created) result; + + Assert.NotNull(createdResult); Assert.NotNull(createdResult.Location); - Assert.IsAssignableFrom(createdResult.Value); Assert.NotEmpty(todos); Assert.Collection(todos, todo => @@ -197,12 +211,15 @@ public class TodoMoqTests .Returns(Task.CompletedTask); //Act - var createdResult = (Created)await TodoEndpointsV2.UpdateTodo(updatedTodo, mock.Object); + var result = await TodoEndpointsV2.UpdateTodo(updatedTodo, mock.Object); //Assert - Assert.Equal(201, createdResult.StatusCode); + Assert.IsType, NotFound>>(result); + + var createdResult = (Created) result.Result; + + Assert.NotNull(createdResult); Assert.NotNull(createdResult.Location); - Assert.IsAssignableFrom(createdResult.Value); Assert.Equal("Updated test title", existingTodo.Title); Assert.True(existingTodo.IsDone); @@ -231,10 +248,14 @@ public class TodoMoqTests .Returns(Task.CompletedTask); //Act - var noContentResult = (NoContent)await TodoEndpointsV2.DeleteTodo(existingTodo.Id, mock.Object); + var result = await TodoEndpointsV2.DeleteTodo(existingTodo.Id, mock.Object); //Assert - Assert.Equal(204, noContentResult.StatusCode); + Assert.IsType>(result); + + var noContentResult = (NoContent) result.Result; + + Assert.NotNull(noContentResult); Assert.Empty(todos); } } diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV1.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV1.cs index a03b8b2..6b29daa 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV1.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV1.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.EntityFrameworkCore; using WebMinRouteGroup.Data; @@ -38,7 +39,7 @@ public static class TodoEndpointsV1 } // get todo by id - public static async Task GetTodo(int id, TodoGroupDbContext database) + public static async Task, NotFound>> GetTodo(int id, TodoGroupDbContext database) { var todo = await database.Todos.FindAsync(id); @@ -67,7 +68,7 @@ public static class TodoEndpointsV1 } // update todo - public static async Task UpdateTodo(Todo todo, TodoGroupDbContext database) + public static async Task, NotFound>> UpdateTodo(Todo todo, TodoGroupDbContext database) { var existingTodo = await database.Todos.FindAsync(todo.Id); @@ -86,7 +87,7 @@ public static class TodoEndpointsV1 } // delete todo - public static async Task DeleteTodo(int id, TodoGroupDbContext database) + public static async Task> DeleteTodo(int id, TodoGroupDbContext database) { var todo = await database.Todos.FindAsync(id); diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV2.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV2.cs index 30c3e5e..3d6089d 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV2.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/TodoEndpointsV2.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Http.HttpResults; using WebMinRouteGroup.Data; using WebMinRouteGroup.Services; @@ -46,7 +47,7 @@ public static class TodoEndpointsV2 } // get todo by id - public static async Task GetTodo(int id, ITodoService todoService) + public static async Task, NotFound>> GetTodo(int id, ITodoService todoService) { var todo = await todoService.Find(id); @@ -74,7 +75,7 @@ public static class TodoEndpointsV2 } // update todo - public static async Task UpdateTodo(Todo todo, ITodoService todoService) + public static async Task, NotFound>> UpdateTodo(Todo todo, ITodoService todoService) { var existingTodo = await todoService.Find(todo.Id); @@ -93,7 +94,7 @@ public static class TodoEndpointsV2 } // delete todo - public static async Task DeleteTodo(int id, ITodoService todoService) + public static async Task> DeleteTodo(int id, ITodoService todoService) { var todo = await todoService.Find(id);