From 6c9719d60ad88368bfa5b62b8133ce065f6cc5eb Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 14 Jan 2021 17:37:20 +0200 Subject: Refactoed technology service tests to use Assert.Throw instead of try catch --- src/DevHive.Services/Services/TechnologyService.cs | 1 - .../TechnologyServices.Tests.cs | 129 +++++++-------------- .../DevHive.Web.Tests/DevHive.Web.Tests.csproj | 1 + .../TechnologyController.Tests.cs | 55 +++++++++ .../Controllers/TechnologyController.cs | 6 +- src/DevHive.sln | 11 +- 6 files changed, 109 insertions(+), 94 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs (limited to 'src') diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index bdcf70f..7fd0b2f 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -65,7 +65,6 @@ namespace DevHive.Services.Services #endregion #region Delete - public async Task DeleteTechnology(Guid technologyId) { if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId)) diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index 9e0876d..1f0b01e 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -58,32 +58,23 @@ namespace DevHive.Services.Tests [Test] public void Create_ThrowsArgumentException_WhenEntityAlreadyExists() { - Task.Run(async () => + string exceptionMessage = "Technology already exists!"; + string technologyName = "Gosho Trapov"; + + CreateTechnologyServiceModel createTechnologyServiceModel = new() { - string expectedExceptionMessage = "Technology already exists!"; - string technologyName = "Gosho Trapov"; + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName + }; - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = technologyName - }; - Technology technology = new() - { - Name = technologyName - }; + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.Create(createTechnologyServiceModel)); - try - { - await this.TechnologyService.Create(createTechnologyServiceModel); - Assert.Fail("Create does not throw exception when technology already exists"); - } - catch (ArgumentException ex) - { - Assert.AreEqual(expectedExceptionMessage, ex.Message); - } - }).GetAwaiter().GetResult(); + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion @@ -117,22 +108,13 @@ namespace DevHive.Services.Tests [Test] public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() { - Task.Run(async () => - { - string exceptionMessage = "The technology does not exist"; - Guid id = new Guid(); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + string exceptionMessage = "The technology does not exist"; + Guid id = new Guid(); + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - try - { - await this.TechnologyService.GetTechnologyById(id); - Assert.Fail("GetTechnologyById does not throw exception when technology does not exist"); - } - catch (ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception messege is nto correct"); - } - }).GetAwaiter().GetResult(); + Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.GetTechnologyById(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion @@ -170,52 +152,34 @@ namespace DevHive.Services.Tests [Test] public void UpdateTechnology_ThrowsException_WhenTechnologyDoesNotExist() { - Task.Run(async () => + string exceptionMessage = "Technology does not exist!"; + Guid id = new Guid(); + UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel { - string exceptionMessage = "Technology does not exist!"; - Guid id = new Guid(); - UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel - { - }; + }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - try - { - await this.TechnologyService.UpdateTechnology(id, updateTechnologyServiceModel); - Assert.Fail("UpdateTechnology does not throw exception when technology does not exist"); - } - catch (ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); - } - }).GetAwaiter().GetResult(); + Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.UpdateTechnology(id, updateTechnologyServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } [Test] public void UpdateTechnology_ThrowsException_WhenTechnologyNameAlreadyExists() { - Task.Run(async () => + string exceptionMessage = "Technology name already exists!"; + Guid id = new Guid(); + UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel { - string exceptionMessage = "Technology name already exists!"; - Guid id = new Guid(); - UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel - { - }; + }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - try - { - await this.TechnologyService.UpdateTechnology(id, updateTechnologyServiceModel); - Assert.Fail("UpdateTechnology does not throw exception when technology name already exist"); - } - catch (ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); - } - }).GetAwaiter().GetResult(); + Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.UpdateTechnology(id, updateTechnologyServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion @@ -244,23 +208,14 @@ namespace DevHive.Services.Tests [Test] public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist() { - Task.Run(async () => - { - string exceptionMessage = "Technology does not exist!"; - Guid id = new Guid(); + string exceptionMessage = "Technology does not exist!"; + Guid id = new Guid(); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - try - { - await this.TechnologyService.DeleteTechnology(id); - Assert.Fail("DeleteTechnology does not throw exception when technology does not exist"); - } - catch (ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - }).GetAwaiter().GetResult(); + Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.DeleteTechnology(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion //Task.Run(async () => diff --git a/src/DevHive.Tests/DevHive.Web.Tests/DevHive.Web.Tests.csproj b/src/DevHive.Tests/DevHive.Web.Tests/DevHive.Web.Tests.csproj index e527713..47f40ee 100644 --- a/src/DevHive.Tests/DevHive.Web.Tests/DevHive.Web.Tests.csproj +++ b/src/DevHive.Tests/DevHive.Web.Tests/DevHive.Web.Tests.csproj @@ -7,6 +7,7 @@ + diff --git a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs new file mode 100644 index 0000000..711f66f --- /dev/null +++ b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs @@ -0,0 +1,55 @@ +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Technology; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; +using System.Threading.Tasks; + +namespace DevHive.Web.Tests +{ + [TestFixture] + public class TechnologyControllerTests + { + private Mock TechnologyServiceMock { get; set; } + private Mock MapperMock { get; set; } + private TechnologyController TechnologyController { get; set; } + + [SetUp] + public void SetUp() + { + this.TechnologyServiceMock = new Mock(); + this.MapperMock = new Mock(); + this.TechnologyController = new TechnologyController(this.TechnologyServiceMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + public void Create_ReturnsOkObjectResult_WhenTechnologyIsSuccessfullyCreated() + { + string name = "Gosho Trapov"; + + CreateTechnologyWebModel createTechnologyWebModel = new CreateTechnologyWebModel + { + Name = name + }; + CreateTechnologyServiceModel createTechnologyServiceModel = new CreateTechnologyServiceModel + { + Name = name + }; + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); + this.TechnologyServiceMock.Setup(p => p.Create(It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.TechnologyController.Create(createTechnologyWebModel).Result; + + Assert.IsInstanceOf(result); + } + + + #endregion + + } +} diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index ecac318..6c51730 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -37,10 +37,10 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid technologyId) { - TechnologyServiceModel technologyServiceModel = await this._technologyService.GetTechnologyById(technologyId); - TechnologyWebModel technologyWebModel = this._technologyMapper.Map(technologyServiceModel); + CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(technologyId); + CreateTechnologyWebModel createTechnologyWebModel = this._technologyMapper.Map(createTechnologyServiceModel); - return new OkObjectResult(technologyWebModel); + return new OkObjectResult(createTechnologyWebModel); } [HttpPut] diff --git a/src/DevHive.sln b/src/DevHive.sln index 19ebbc2..aa58d9c 100644 --- a/src/DevHive.sln +++ b/src/DevHive.sln @@ -13,11 +13,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevHive.Common", "DevHive.C EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DevHive.Tests", "DevHive.Tests", "{8ED705F9-7038-472C-B53F-5B1480A74A37}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevHive.Data.Tests", "DevHive.Tests\DevHive.Data.Tests\DevHive.Data.Tests.csproj", "{346876CE-2C9B-4538-BE82-EA2017F7D405}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevHive.Data.Tests", "DevHive.Tests\DevHive.Data.Tests\DevHive.Data.Tests.csproj", "{346876CE-2C9B-4538-BE82-EA2017F7D405}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevHive.Services.Tests", "DevHive.Tests\DevHive.Services.Tests\DevHive.Services.Tests.csproj", "{9BBB8A48-C5AF-4F35-925F-3404A74E47F4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevHive.Services.Tests", "DevHive.Tests\DevHive.Services.Tests\DevHive.Services.Tests.csproj", "{9BBB8A48-C5AF-4F35-925F-3404A74E47F4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevHive.Web.Tests", "DevHive.Tests\DevHive.Web.Tests\DevHive.Web.Tests.csproj", "{2574CDBE-CC99-4BF8-BF7F-34C131788036}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevHive.Web.Tests", "DevHive.Tests\DevHive.Web.Tests\DevHive.Web.Tests.csproj", "{2574CDBE-CC99-4BF8-BF7F-34C131788036}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2E5E3F99-7936-4F3B-974E-A98000D1564B}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution -- cgit v1.2.3