aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-03-15 09:27:12 +0200
committertranstrike <transtrike@gmail.com>2021-03-15 09:27:12 +0200
commit0161be09312fde634865f110504884119a617d5c (patch)
tree0fa68366edcb024c054f370ecf90f5b66282aae5 /src/Services/DevHive.Services.Tests/RoleService.Tests.cs
parente3b5757b5a5db2f7874b0924cdd4a22b1a9e1ee2 (diff)
parentac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde (diff)
downloadDevHive-0161be09312fde634865f110504884119a617d5c.tar
DevHive-0161be09312fde634865f110504884119a617d5c.tar.gz
DevHive-0161be09312fde634865f110504884119a617d5c.zip
Fixed to new() where possible and readable
Diffstat (limited to 'src/Services/DevHive.Services.Tests/RoleService.Tests.cs')
-rw-r--r--src/Services/DevHive.Services.Tests/RoleService.Tests.cs136
1 files changed, 88 insertions, 48 deletions
diff --git a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
index 29d5df2..83dabf9 100644
--- a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
@@ -13,17 +13,17 @@ namespace DevHive.Services.Tests
[TestFixture]
public class RoleServiceTests
{
- private Mock<IRoleRepository> RoleRepositoryMock { get; set; }
- private Mock<IMapper> MapperMock { get; set; }
- private RoleService RoleService { get; set; }
+ private Mock<IRoleRepository> _roleRepositoryMock;
+ private Mock<IMapper> _mapperMock;
+ private RoleService _roleService;
#region SetUps
[SetUp]
public void Setup()
{
- this.RoleRepositoryMock = new Mock<IRoleRepository>();
- this.MapperMock = new Mock<IMapper>();
- this.RoleService = new RoleService(this.RoleRepositoryMock.Object, this.MapperMock.Object);
+ this._roleRepositoryMock = new Mock<IRoleRepository>();
+ this._mapperMock = new Mock<IMapper>();
+ this._roleService = new RoleService(this._roleRepositoryMock.Object, this._mapperMock.Object);
}
#endregion
@@ -43,12 +43,20 @@ namespace DevHive.Services.Tests
Id = id
};
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Role>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny<string>())).Returns(Task.FromResult(role));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())).Returns(role);
-
- Guid result = await this.RoleService.CreateRole(createRoleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Role>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByNameAsync(It.IsAny<string>()))
+ .ReturnsAsync(role);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>()))
+ .Returns(role);
+
+ Guid result = await this._roleService.CreateRole(createRoleServiceModel);
Assert.AreEqual(id, result);
}
@@ -67,11 +75,17 @@ namespace DevHive.Services.Tests
Name = roleName
};
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Role>())).Returns(Task.FromResult(false));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())).Returns(role);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.AddAsync(It.IsAny<Role>()))
+ .ReturnsAsync(false);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>()))
+ .Returns(role);
- Guid result = await this.RoleService.CreateRole(createRoleServiceModel);
+ Guid result = await this._roleService.CreateRole(createRoleServiceModel);
Assert.IsTrue(result == Guid.Empty);
}
@@ -86,14 +100,12 @@ namespace DevHive.Services.Tests
{
Name = roleName
};
- Role role = new Role
- {
- Name = roleName
- };
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.CreateRole(createRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.CreateRole(createRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -103,7 +115,7 @@ namespace DevHive.Services.Tests
[Test]
public async Task GetRoleById_ReturnsTheRole_WhenItExists()
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
string name = "Gosho Trapov";
Role role = new Role
{
@@ -114,10 +126,14 @@ namespace DevHive.Services.Tests
Name = name
};
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(role));
- this.MapperMock.Setup(p => p.Map<RoleServiceModel>(It.IsAny<Role>())).Returns(roleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(role);
+ this._mapperMock
+ .Setup(p => p.Map<RoleServiceModel>(It.IsAny<Role>()))
+ .Returns(roleServiceModel);
- RoleServiceModel result = await this.RoleService.GetRoleById(id);
+ RoleServiceModel result = await this._roleService.GetRoleById(id);
Assert.AreEqual(name, result.Name);
}
@@ -126,10 +142,12 @@ namespace DevHive.Services.Tests
public void GetRoleById_ThrowsException_WhenRoleDoesNotExist()
{
string exceptionMessage = "Role does not exist!";
- Guid id = new Guid();
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Role>(null));
+ Guid id = Guid.NewGuid();
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .Returns(Task.FromResult<Role>(null));
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.GetRoleById(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.GetRoleById(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -153,12 +171,20 @@ namespace DevHive.Services.Tests
Name = name,
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(false));
- this.RoleRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Role>())).Returns(Task.FromResult(shouldPass));
- this.MapperMock.Setup(p => p.Map<Role>(It.IsAny<UpdateRoleServiceModel>())).Returns(role);
-
- bool result = await this.RoleService.UpdateRole(updateRoleServiceModel);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(false);
+ this._roleRepositoryMock
+ .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Role>()))
+ .ReturnsAsync(shouldPass);
+ this._mapperMock
+ .Setup(p => p.Map<Role>(It.IsAny<UpdateRoleServiceModel>()))
+ .Returns(role);
+
+ bool result = await this._roleService.UpdateRole(updateRoleServiceModel);
Assert.AreEqual(shouldPass, result);
}
@@ -171,9 +197,11 @@ namespace DevHive.Services.Tests
{
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.UpdateRole(updateRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -186,10 +214,14 @@ namespace DevHive.Services.Tests
{
};
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny<string>())).Returns(Task.FromResult(true));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.DoesNameExist(It.IsAny<string>()))
+ .ReturnsAsync(true);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.UpdateRole(updateRoleServiceModel));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.UpdateRole(updateRoleServiceModel));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}
@@ -201,14 +233,20 @@ namespace DevHive.Services.Tests
[TestCase(false)]
public async Task DeleteRole_ShouldReturnIfDeletionIsSuccessfull_WhenRoleExists(bool shouldPass)
{
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
Role role = new Role();
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
- this.RoleRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(role));
- this.RoleRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Role>())).Returns(Task.FromResult(shouldPass));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(true);
+ this._roleRepositoryMock
+ .Setup(p => p.GetByIdAsync(It.IsAny<Guid>()))
+ .ReturnsAsync(role);
+ this._roleRepositoryMock
+ .Setup(p => p.DeleteAsync(It.IsAny<Role>()))
+ .ReturnsAsync(shouldPass);
- bool result = await this.RoleService.DeleteRole(id);
+ bool result = await this._roleService.DeleteRole(id);
Assert.AreEqual(shouldPass, result);
}
@@ -217,11 +255,13 @@ namespace DevHive.Services.Tests
public void DeleteRole_ThrowsException_WhenRoleDoesNotExist()
{
string exceptionMessage = "Role does not exist!";
- Guid id = new Guid();
+ Guid id = Guid.NewGuid();
- this.RoleRepositoryMock.Setup(p => p.DoesRoleExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this._roleRepositoryMock
+ .Setup(p => p.DoesRoleExist(It.IsAny<Guid>()))
+ .ReturnsAsync(false);
- Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RoleService.DeleteRole(id));
+ Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._roleService.DeleteRole(id));
Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message");
}