diff options
| author | transtrike <transtrike@gmail.com> | 2021-03-15 09:27:12 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-03-15 09:27:12 +0200 |
| commit | 0161be09312fde634865f110504884119a617d5c (patch) | |
| tree | 0fa68366edcb024c054f370ecf90f5b66282aae5 /src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs | |
| parent | e3b5757b5a5db2f7874b0924cdd4a22b1a9e1ee2 (diff) | |
| parent | ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde (diff) | |
| download | DevHive-0161be09312fde634865f110504884119a617d5c.tar DevHive-0161be09312fde634865f110504884119a617d5c.tar.gz DevHive-0161be09312fde634865f110504884119a617d5c.zip | |
Fixed to new() where possible and readable
Diffstat (limited to 'src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs')
| -rw-r--r-- | src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs | 156 |
1 files changed, 102 insertions, 54 deletions
diff --git a/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs index a43d4b9..f9c599c 100644 --- a/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/Services/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -14,17 +14,17 @@ namespace DevHive.Services.Tests [TestFixture] public class TechnologyServicesTests { - private Mock<ITechnologyRepository> TechnologyRepositoryMock { get; set; } - private Mock<IMapper> MapperMock { get; set; } - private TechnologyService TechnologyService { get; set; } + private Mock<ITechnologyRepository> _technologyRepositoryMock; + private Mock<IMapper> _mapperMock; + private TechnologyService _technologyService; #region SetUps [SetUp] public void Setup() { - this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>(); - this.MapperMock = new Mock<IMapper>(); - this.TechnologyService = new TechnologyService(this.TechnologyRepositoryMock.Object, this.MapperMock.Object); + this._technologyRepositoryMock = new Mock<ITechnologyRepository>(); + this._mapperMock = new Mock<IMapper>(); + this._technologyService = new TechnologyService(this._technologyRepositoryMock.Object, this._mapperMock.Object); } #endregion @@ -44,12 +44,20 @@ namespace DevHive.Services.Tests Id = id }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Technology>())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())).Returns(technology); - - Guid result = await this.TechnologyService.CreateTechnology(createTechnologyServiceModel); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._technologyRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Technology>())) + .ReturnsAsync(true); + this._technologyRepositoryMock + .Setup(p => p.GetByNameAsync(It.IsAny<string>())) + .ReturnsAsync(technology); + this._mapperMock + .Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())) + .Returns(technology); + + Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel); Assert.AreEqual(id, result); } @@ -68,11 +76,17 @@ namespace DevHive.Services.Tests Name = technologyName }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Technology>())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())).Returns(technology); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._technologyRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Technology>())) + .ReturnsAsync(false); + this._mapperMock + .Setup(p => p.Map<Technology>(It.IsAny<CreateTechnologyServiceModel>())) + .Returns(technology); - Guid result = await this.TechnologyService.CreateTechnology(createTechnologyServiceModel); + Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel); Assert.IsTrue(result == Guid.Empty); } @@ -87,14 +101,12 @@ namespace DevHive.Services.Tests { 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>())) + .ReturnsAsync(true); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.CreateTechnology(createTechnologyServiceModel)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.CreateTechnology(createTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -104,7 +116,7 @@ namespace DevHive.Services.Tests [Test] public async Task GetTechnologyById_ReturnsTheTechnology_WhenItExists() { - Guid id = new Guid(); + Guid id = Guid.NewGuid(); string name = "Gosho Trapov"; Technology technology = new() { @@ -115,10 +127,14 @@ namespace DevHive.Services.Tests Name = name }; - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map<ReadTechnologyServiceModel>(It.IsAny<Technology>())).Returns(readTechnologyServiceModel); + this._technologyRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(technology); + this._mapperMock + .Setup(p => p.Map<ReadTechnologyServiceModel>(It.IsAny<Technology>())) + .Returns(readTechnologyServiceModel); - ReadTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); + ReadTechnologyServiceModel result = await this._technologyService.GetTechnologyById(id); Assert.AreEqual(name, result.Name); } @@ -127,10 +143,12 @@ namespace DevHive.Services.Tests public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() { 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)); + Guid id = Guid.NewGuid(); + this._technologyRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<Technology>(null)); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.GetTechnologyById(id)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.GetTechnologyById(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -146,10 +164,14 @@ namespace DevHive.Services.Tests technologies.Add(firstTechnology); technologies.Add(secondTechnology); - this.TechnologyRepositoryMock.Setup(p => p.GetTechnologies()).Returns(new HashSet<Technology>()); - this.MapperMock.Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())).Returns(technologies); + this._technologyRepositoryMock + .Setup(p => p.GetTechnologies()) + .Returns(new HashSet<Technology>()); + this._mapperMock + .Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())) + .Returns(technologies); - HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies(); + HashSet<ReadTechnologyServiceModel> result = this._technologyService.GetTechnologies(); Assert.GreaterOrEqual(2, result.Count, "GetTechnologies does not return all technologies"); } @@ -157,10 +179,14 @@ namespace DevHive.Services.Tests [Test] public void GetLanguages_ReturnsEmptyHashSet_IfNoLanguagesExist() { - this.TechnologyRepositoryMock.Setup(p => p.GetTechnologies()).Returns(new HashSet<Technology>()); - this.MapperMock.Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())).Returns(new HashSet<ReadTechnologyServiceModel>()); + this._technologyRepositoryMock + .Setup(p => p.GetTechnologies()) + .Returns(new HashSet<Technology>()); + this._mapperMock + .Setup(p => p.Map<HashSet<ReadTechnologyServiceModel>>(It.IsAny<HashSet<Technology>>())) + .Returns(new HashSet<ReadTechnologyServiceModel>()); - HashSet<ReadTechnologyServiceModel> result = this.TechnologyService.GetTechnologies(); + HashSet<ReadTechnologyServiceModel> result = this._technologyService.GetTechnologies(); Assert.IsEmpty(result, "GetTechnologies does not return empty string when no technologies exist"); } @@ -184,12 +210,20 @@ namespace DevHive.Services.Tests Name = name, }; - 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(false)); - this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Technology>())).Returns(Task.FromResult(shouldPass)); - this.MapperMock.Setup(p => p.Map<Technology>(It.IsAny<UpdateTechnologyServiceModel>())).Returns(technology); - - bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())) + .ReturnsAsync(false); + this._technologyRepositoryMock + .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Technology>())) + .ReturnsAsync(shouldPass); + this._mapperMock + .Setup(p => p.Map<Technology>(It.IsAny<UpdateTechnologyServiceModel>())) + .Returns(technology); + + bool result = await this._technologyService.UpdateTechnology(updatetechnologyServiceModel); Assert.AreEqual(shouldPass, result); } @@ -202,9 +236,11 @@ namespace DevHive.Services.Tests { }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -217,10 +253,14 @@ namespace DevHive.Services.Tests { }; - 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>())) + .ReturnsAsync(true); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny<string>())) + .ReturnsAsync(true); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -233,14 +273,20 @@ namespace DevHive.Services.Tests [TestCase(false)] public async Task DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) { - Guid id = new Guid(); + Guid id = Guid.NewGuid(); Technology technology = new Technology(); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(technology)); - this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Technology>())).Returns(Task.FromResult(shouldPass)); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._technologyRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(technology); + this._technologyRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny<Technology>())) + .ReturnsAsync(shouldPass); - bool result = await this.TechnologyService.DeleteTechnology(id); + bool result = await this._technologyService.DeleteTechnology(id); Assert.AreEqual(shouldPass, result); } @@ -249,11 +295,13 @@ namespace DevHive.Services.Tests public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist() { string exceptionMessage = "Technology does not exist!"; - Guid id = new Guid(); + Guid id = Guid.NewGuid(); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this._technologyRepositoryMock + .Setup(p => p.DoesTechnologyExistAsync(It.IsAny<Guid>())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.TechnologyService.DeleteTechnology(id)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._technologyService.DeleteTechnology(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } |
