aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanail Dimitrov <danaildimitrov321@gmail.com>2021-01-14 17:37:20 +0200
committerDanail Dimitrov <danaildimitrov321@gmail.com>2021-01-14 17:37:20 +0200
commit6c9719d60ad88368bfa5b62b8133ce065f6cc5eb (patch)
tree393f7e194a9c21d130f341e7c1a4bff7fe356f76 /src
parent9ab6d2f2b330a3b49435f8e1ddd02386aa6479ae (diff)
downloadDevHive-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')
-rw-r--r--src/DevHive.Services/Services/TechnologyService.cs1
-rw-r--r--src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs129
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/DevHive.Web.Tests.csproj1
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs55
-rw-r--r--src/DevHive.Web/Controllers/TechnologyController.cs6
-rw-r--r--src/DevHive.sln11
6 files changed, 109 insertions, 94 deletions
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<bool> 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<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
+
+ }
+}
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<IActionResult> GetById(Guid technologyId)
{
- TechnologyServiceModel technologyServiceModel = await this._technologyService.GetTechnologyById(technologyId);
- TechnologyWebModel technologyWebModel = this._technologyMapper.Map<TechnologyWebModel>(technologyServiceModel);
+ CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(technologyId);
+ CreateTechnologyWebModel createTechnologyWebModel = this._technologyMapper.Map<CreateTechnologyWebModel>(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