public void DeleteVote_NonexistentCommentVote_ThrowsCommentVoteNotFoundException() { // Arrange int userId = 1; int commentId = 2; Comment comment = new Comment { CommentId = commentId, CommentVotes = { new CommentVote { CommentId = commentId, UserId = userId + 1 }, new CommentVote { CommentId = commentId, UserId = userId + 2 } } }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<CommentVoteNotFoundException>(() => target.DeleteVote(commentId, userId)); commentRepositoryMock.Verify( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void DeleteVote_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException() { // Arrange int commentId = 1; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentOutOfRangeException>(() => target.DeleteVote(commentId, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => target.DeleteVote(commentId, 0)); }
public void DeleteVote_AllCredentialsAreValid_DeletesCommentVote() { // Arrange int commentId = 1; int userId = 2; CommentVote[] commentVotes = { new CommentVote { CommentId = commentId, UserId = userId }, new CommentVote { CommentId = commentId, UserId = userId + 1 }, new CommentVote { CommentId = commentId, UserId = userId + 2 } }; Comment comment = new Comment { CommentId = commentId, CommentVotes = commentVotes.ToList() }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentVotes }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); IEnumerable<CommentVote> newCommentVotes = null; commentRepositoryMock.Setup(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId))) .Callback((Comment c) => newCommentVotes = c.CommentVotes); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act target.DeleteVote(comment.CommentId, userId); // Assert Assert.AreEqual(commentVotes.Count() - 1, newCommentVotes.Count()); Assert.AreEqual(0, newCommentVotes.Count(v => v.CommentId == commentId && v.UserId == userId)); commentRepositoryMock.Verify( r => r.GetById(commentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude))), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); }