diff options
Diffstat (limited to 'src')
3 files changed, 247 insertions, 127 deletions
diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs index 17616fe..2da90d9 100644 --- a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs @@ -12,8 +12,8 @@ namespace DevHive.Data.Tests [TestFixture] public class RatingRepositoryTests { - private DevHiveContext Context { get; set; } - private RatingRepository RatingRepository { get; set; } + private DevHiveContext _context; + private RatingRepository _ratingRepository; #region Setups [SetUp] @@ -22,14 +22,14 @@ namespace DevHive.Data.Tests var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); - this.RatingRepository = new RatingRepository(this.Context, null); + this._context = new DevHiveContext(optionsBuilder.Options); + this._ratingRepository = new RatingRepository(this._context, null); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -40,7 +40,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - Rating ratingResult = await this.RatingRepository.GetByIdAsync(ratingId); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId); Assert.AreEqual(ratingResult.Id, ratingId); } @@ -48,7 +48,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist() { - Rating ratingResult = await this.RatingRepository.GetByIdAsync(Guid.NewGuid()); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid()); Assert.IsNull(ratingResult); } @@ -63,7 +63,7 @@ namespace DevHive.Data.Tests await AddDummyRating(Guid.NewGuid(), postId); await AddDummyRating(Guid.NewGuid(), postId); - List<Rating> result = await this.RatingRepository.GetRatingsByPostId(postId); + List<Rating> result = await this._ratingRepository.GetRatingsByPostId(postId); Assert.IsNotEmpty(result); } @@ -71,7 +71,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings() { - List<Rating> result = await this.RatingRepository.GetRatingsByPostId(Guid.NewGuid()); + List<Rating> result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid()); Assert.IsEmpty(result); } @@ -88,7 +88,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(ratingId, postId, userId); - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(userId, postId); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId); Assert.AreEqual(result.Id, ratingId); } @@ -96,7 +96,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist() { - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); Assert.IsNull(result); } @@ -112,7 +112,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(Guid.NewGuid(), postId, userId); - bool result = await this.RatingRepository.UserRatedPost(userId, postId); + bool result = await this._ratingRepository.UserRatedPost(userId, postId); Assert.IsTrue(result); } @@ -120,7 +120,7 @@ namespace DevHive.Data.Tests [Test] public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost() { - bool result = await this.RatingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); + bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); Assert.IsFalse(result); } @@ -133,7 +133,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - bool result = await this.RatingRepository.DoesRatingExist(ratingId); + bool result = await this._ratingRepository.DoesRatingExist(ratingId); Assert.IsTrue(result); } @@ -141,7 +141,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist() { - bool result = await this.RatingRepository.DoesRatingExist(Guid.NewGuid()); + bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid()); Assert.IsFalse(result); } @@ -153,12 +153,12 @@ namespace DevHive.Data.Tests Rating rating = new Rating { Id = ratingId, - Post = this.Context.Posts.FirstOrDefault(x => x.Id == postId), - User = this.Context.Users.FirstOrDefault(x => x.Id == userId) + Post = this._context.Posts.FirstOrDefault(x => x.Id == postId), + User = this._context.Users.FirstOrDefault(x => x.Id == userId) }; - await this.Context.Rating.AddAsync(rating); - await this.Context.SaveChangesAsync(); + await this._context.Rating.AddAsync(rating); + await this._context.SaveChangesAsync(); } private async Task AddDummyPost(Guid postId) @@ -169,8 +169,8 @@ namespace DevHive.Data.Tests Message = "Never gonna give you up" }; - await this.Context.Posts.AddAsync(post); - await this.Context.SaveChangesAsync(); + await this._context.Posts.AddAsync(post); + await this._context.SaveChangesAsync(); } private async Task AddDummyUser(Guid userId) @@ -180,8 +180,8 @@ namespace DevHive.Data.Tests Id = userId }; - await this.Context.Users.AddAsync(user); - await this.Context.SaveChangesAsync(); + await this._context.Users.AddAsync(user); + await this._context.SaveChangesAsync(); } #endregion } diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs index 89c1a2e..5f84530 100644 --- a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs @@ -13,21 +13,21 @@ namespace DevHive.Services.Tests [TestFixture] public class RatingServiceTests { - private Mock<IPostRepository> PostRepositoryMock { get; set; } - private Mock<IRatingRepository> RatingRepositoryMock { get; set; } - private Mock<IUserRepository> UserRepositoryMock { get; set; } - private Mock<IMapper> MapperMock { get; set; } - private RatingService RatingService { get; set; } + private Mock<IPostRepository> _postRepositoryMock; + private Mock<IRatingRepository> _ratingRepositoryMock; + private Mock<IUserRepository> _userRepositoryMock; + private Mock<IMapper> _mapperMock; + private RatingService _ratingService; #region SetUps [SetUp] public void SetUp() { - this.PostRepositoryMock = new Mock<IPostRepository>(); - this.RatingRepositoryMock = new Mock<IRatingRepository>(); - this.UserRepositoryMock = new Mock<IUserRepository>(); - this.MapperMock = new Mock<IMapper>(); - this.RatingService = new RatingService(this.PostRepositoryMock.Object, this.RatingRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + this._postRepositoryMock = new Mock<IPostRepository>(); + this._ratingRepositoryMock = new Mock<IRatingRepository>(); + this._userRepositoryMock = new Mock<IUserRepository>(); + this._mapperMock = new Mock<IMapper>(); + this._ratingService = new RatingService(this._postRepositoryMock.Object, this._ratingRepositoryMock.Object, this._userRepositoryMock.Object, this._mapperMock.Object); } #endregion @@ -59,15 +59,29 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Rating>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Rating>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(id, result); } @@ -99,14 +113,26 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Rating>())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Rating>())) + .ReturnsAsync(false); + this._mapperMock + .Setup(p => p.Map<Rating>(It.IsAny<CreateRatingServiceModel>())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(result, Guid.Empty); } @@ -134,10 +160,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingById(id); + ReadRatingServiceModel result = await this._ratingService.GetRatingById(id); Assert.AreEqual(isLike, result.IsLike); } @@ -146,9 +176,11 @@ namespace DevHive.Services.Tests public void GetRatingById_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Rating>(null)); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<Rating>(null)); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -174,10 +206,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingByPostAndUser(user.Id, Guid.Empty); + ReadRatingServiceModel result = await this._ratingService.GetRatingByPostAndUser(user.Id, Guid.Empty); Assert.AreEqual(isLike, result.IsLike); } @@ -186,9 +222,11 @@ namespace DevHive.Services.Tests public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult<Rating>(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .Returns(Task.FromResult<Rating>(null)); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -221,13 +259,23 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())).Returns(readRatingServiceModel); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>())) + .ReturnsAsync(true); + this._mapperMock + .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<Rating>())) + .Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.AreEqual(result, readRatingServiceModel); } @@ -258,12 +306,20 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>())).Returns(Task.FromResult(false)); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Rating>())) + .ReturnsAsync(false); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.IsNull(result); } @@ -278,9 +334,11 @@ namespace DevHive.Services.Tests IsLike = true }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult<Rating>(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .Returns(Task.FromResult<Rating>(null)); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -307,11 +365,17 @@ namespace DevHive.Services.Tests User = user }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -327,11 +391,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Rating>(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Rating>())).Returns(Task.FromResult(true)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<Rating>(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny<Rating>())) + .ReturnsAsync(true); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsTrue(result); } @@ -345,11 +415,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Rating>(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Rating>())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<Rating>(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny<Rating>())) + .ReturnsAsync(false); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsFalse(result); } @@ -359,9 +435,11 @@ namespace DevHive.Services.Tests { string exceptionMessage = "Rating does not exist!"; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny<Guid>())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.RatingService.DeleteRating(Guid.Empty)); + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this._ratingService.DeleteRating(Guid.Empty)); Assert.AreEqual(ex.Message, exceptionMessage); } diff --git a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs index dd8954f..c7340a6 100644 --- a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs @@ -16,18 +16,18 @@ namespace DevHive.Web.Tests [TestFixture] public class RatingControllerTests { - private Mock<IRatingService> RatingServiceMock { get; set; } - private Mock<IMapper> MapperMock { get; set; } - private Mock<IJwtService> JwtServiceMock { get; set; } - private RatingController RatingController { get; set; } + private Mock<IRatingService> _ratingServiceMock; + private Mock<IMapper> _mapperMock; + private Mock<IJwtService> _jwtServiceMock; + private RatingController _ratingController; [SetUp] public void SetUp() { - this.RatingServiceMock = new Mock<IRatingService>(); - this.MapperMock = new Mock<IMapper>(); - this.JwtServiceMock = new Mock<IJwtService>(); - this.RatingController = new RatingController(this.RatingServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); + this._ratingServiceMock = new Mock<IRatingService>(); + this._mapperMock = new Mock<IMapper>(); + this._jwtServiceMock = new Mock<IJwtService>(); + this._ratingController = new RatingController(this._ratingServiceMock.Object, this._mapperMock.Object, this._jwtServiceMock.Object); } #region Create @@ -47,11 +47,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.MapperMock.Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())).Returns(Task.FromResult(ratingId)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())) + .ReturnsAsync(ratingId); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf<OkObjectResult>(result); @@ -82,11 +88,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.MapperMock.Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())).Returns(Task.FromResult(Guid.Empty)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())) + .ReturnsAsync(Guid.Empty); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf<BadRequestResult>(result); } @@ -114,10 +126,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingById(It.IsAny<Guid>())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingById(It.IsAny<Guid>())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingById(id).Result; + IActionResult result = this._ratingController.GetRatingById(id).Result; Assert.IsInstanceOf<OkObjectResult>(result); } @@ -143,10 +159,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingByPostAndUser(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingByPostAndUser(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingByUserAndPost(userId, postId).Result; + IActionResult result = this._ratingController.GetRatingByUserAndPost(userId, postId).Result; Assert.IsInstanceOf<OkObjectResult>(result); } @@ -186,12 +206,20 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.MapperMock.Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())).Returns(updateRatingServiceModel); - this.MapperMock.Setup(p => p.Map<ReadRatingWebModel>(It.IsAny<ReadRatingServiceModel>())).Returns(readRatingWebModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())).Returns(Task.FromResult(readRatingServiceModel)); - - IActionResult result = this.RatingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())) + .Returns(updateRatingServiceModel); + this._mapperMock + .Setup(p => p.Map<ReadRatingWebModel>(It.IsAny<ReadRatingServiceModel>())) + .Returns(readRatingWebModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf<OkObjectResult>(result); } @@ -210,11 +238,17 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.MapperMock.Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())).Returns(updateRatingServiceModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())).Returns(Task.FromResult<ReadRatingServiceModel>(null)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())) + .Returns(updateRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())) + .Returns(Task.FromResult<ReadRatingServiceModel>(null)); - IActionResult result = this.RatingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf<BadRequestResult>(result); } @@ -226,10 +260,14 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny<Guid>())) + .ReturnsAsync(true); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf<OkResult>(result); } @@ -240,10 +278,14 @@ namespace DevHive.Web.Tests string message = "Could not delete Rating"; Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny<Guid>())) + .ReturnsAsync(false); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf<BadRequestObjectResult>(result); |
