diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-01-14 17:37:20 +0200 |
|---|---|---|
| committer | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-01-14 17:37:20 +0200 |
| commit | 6c9719d60ad88368bfa5b62b8133ce065f6cc5eb (patch) | |
| tree | 393f7e194a9c21d130f341e7c1a4bff7fe356f76 /src/DevHive.Tests | |
| parent | 9ab6d2f2b330a3b49435f8e1ddd02386aa6479ae (diff) | |
| download | DevHive-6c9719d60ad88368bfa5b62b8133ce065f6cc5eb.tar DevHive-6c9719d60ad88368bfa5b62b8133ce065f6cc5eb.tar.gz DevHive-6c9719d60ad88368bfa5b62b8133ce065f6cc5eb.zip | |
Refactoed technology service tests to use Assert.Throw instead of try catch
Diffstat (limited to 'src/DevHive.Tests')
3 files changed, 98 insertions, 87 deletions
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<string>())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => 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<Guid>())).Returns(Task.FromResult<Technology>(null)); + string exceptionMessage = "The technology does not exist"; + Guid id = new Guid(); + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Technology>(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<ArgumentException>(() => 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<Guid>())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).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<ArgumentException>(() => 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<Guid>())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).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<ArgumentException>(() => 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<Guid>())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).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<ArgumentException>(() => 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 @@ </PropertyGroup> <ItemGroup> + <PackageReference Include="Moq" Version="4.15.2" /> <PackageReference Include="NUnit" Version="3.13.0" /> <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> 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<ITechnologyService> TechnologyServiceMock { get; set; } + private Mock<IMapper> MapperMock { get; set; } + private TechnologyController TechnologyController { get; set; } + + [SetUp] + public void SetUp() + { + this.TechnologyServiceMock = new Mock<ITechnologyService>(); + this.MapperMock = new Mock<IMapper>(); + 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<CreateTechnologyServiceModel>(It.IsAny<CreateTechnologyWebModel>())).Returns(createTechnologyServiceModel); + this.TechnologyServiceMock.Setup(p => p.Create(It.IsAny<CreateTechnologyServiceModel>())).Returns(Task.FromResult(true)); + + IActionResult result = this.TechnologyController.Create(createTechnologyWebModel).Result; + + Assert.IsInstanceOf<OkResult>(result); + } + + + #endregion + + } +} |
