aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/RoleService.Tests.cs
diff options
context:
space:
mode:
authorKamen Mladenov <kamen.d.mladenov@protonmail.com>2021-03-14 12:14:59 +0200
committerGitHub <noreply@github.com>2021-03-14 12:14:59 +0200
commit432fc5890814596d50fb409a6e5dc952d7bb4836 (patch)
tree05878ec2aeaa6feda4058e494e586ab0e869a63c /src/Services/DevHive.Services.Tests/RoleService.Tests.cs
parent97459890f8c25b1af2a17109055afea53ee0b280 (diff)
parent10ac1adbf9fbeaca116d3fd356f85dd64c8c2bf8 (diff)
downloadDevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.tar
DevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.tar.gz
DevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.zip
Merge pull request #21 from Team-Kaleidoscope/unit_test_refactoring
Unit test code style refactor
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");
}