From e105e4731d7827c71d16bd07586c4991de9b0c6f Mon Sep 17 00:00:00 2001 From: csimonapastore Date: Sun, 2 Mar 2025 22:51:50 +0100 Subject: [PATCH] Testing exception --- .../Controllers/AuthController_Tests.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/MainProject.Tests/Controllers/AuthController_Tests.cs b/MainProject.Tests/Controllers/AuthController_Tests.cs index 0fecdc6..758f913 100644 --- a/MainProject.Tests/Controllers/AuthController_Tests.cs +++ b/MainProject.Tests/Controllers/AuthController_Tests.cs @@ -173,8 +173,6 @@ public class AuthController_Tests authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny())).ReturnsAsync(authenticatedUser); controller.ModelState.AddModelError("Data", "Invalid data"); ObjectResult response = (ObjectResult)(await controller.AuthenticateAsync(request)); - Console.WriteLine(JsonConvert.SerializeObject(response)); - Console.WriteLine(response.GetType()); Assert.IsInstanceOfType(response, typeof(ObjectResult)); @@ -199,4 +197,43 @@ public class AuthController_Tests } } + [TestMethod] + public async Task AuthenticateAsync_Exception() + { + IConfiguration configuration = TestUtils.CreateConfiguration(); + var authServiceMock = new Mock(); + var controller = new AuthController(configuration, authServiceMock.Object); + + var request = new AuthenticateRequest + { + Data = new AuthenticateRequestData { Username = "user", Password = "pass" } + }; + + authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny())).ThrowsAsync(new Exception("Unexpected error")); + + ObjectResult response = (ObjectResult)(await controller.AuthenticateAsync(request)); + + Assert.IsInstanceOfType(response, typeof(ObjectResult)); + + if(response != null && response.Value != null) + { + Assert.IsTrue(response.StatusCode == 500); + + var result = (BaseResponse)response.Value; + if(result != null) + { + Assert.IsTrue(result.Status == 500); + Assert.IsTrue(result.Message == "Something went wrong. Unexpected error"); + } + else + { + Assert.Fail($"Result value is null"); + } + } + else + { + Assert.Fail($"Response is null"); + } + } + }