public void AddVote_CommentIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException() { // Arrange int userId = 1; bool voteIsPositive = true; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentOutOfRangeException>(() => target.AddVote(-1, userId, voteIsPositive)); Assert.Throws<ArgumentOutOfRangeException>(() => target.AddVote(0, userId, voteIsPositive)); }
public void AddVote_NonexistentCommentId_ThrowsCommentNotFoundException() { // Arrange int commentId = 1; int userId = 1; bool voteIsPositive = true; 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)null); // 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<CommentNotFoundException>(() => target.AddVote(commentId, userId, voteIsPositive)); 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 Delete_PermittedPeriodForDeletingExpired_ThrowsPermittedPeriodForDeletingExpiredException() { // Arrange Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForDeleting + 1)) }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup(r => r.GetById(comment.CommentId)) .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<PermittedPeriodForDeletingExpiredException>(() => target.Delete(comment.CommentId, comment.UserId)); commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.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 Create_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException() { // Arrange int articleId = 1; string commentText = "comment_text"; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentOutOfRangeException>(() => target.Create(-1, articleId, commentText)); Assert.Throws<ArgumentOutOfRangeException>(() => target.Create(0, articleId, commentText)); }
public void Create_CommentTextIsNull_ThrowsArgumentNullException() { // Arrange int userId = 1; int articleId = 2; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentNullException>(() => target.Create(userId, articleId, null)); }
public void CanHaveReply_CommentTreeLevelIsLessThanMaxCommentTreeLevel_ReturnsTrue() { // Arrange Comment comment = new Comment { TreeLevel = (byte)(this._serviceSettings.MaxCommentTreeLevel - 1) }; // Arrange - create target ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanHaveReply(comment); // Assert Assert.IsTrue(result); }
public void CanHaveReply_CommentIsNull_ThrowsArgumentNullException() { // Arrange ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentNullException>(() => target.CanHaveReply(null)); }
public void CanEdit_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException() { // Arrange Comment comment = new Comment { CommentId = 1 }; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentOutOfRangeException>(() => target.CanEdit(-1, comment)); Assert.Throws<ArgumentOutOfRangeException>(() => target.CanEdit(0, comment)); }
public void Reply_CommentTextIsEmpty_ThrowsArgumentException() { // Arrange int commentId = 1; int userId = 2; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentException>(() => target.Reply(commentId, userId, "")); }
public void Reply_AllCredentialsAreValid_CreatesAndReturnsReply() { // Arrange User user = new User { UserId = 1 }; string commentText = "comment_text"; string validatedCommentText = "validated_comment_text"; Comment[] commentReplies = { new Comment { CommentId = 2 }, new Comment { CommentId = 3 } }; Comment comment = new Comment { CommentId = 4, ArticleId = 5, Comment1 = commentReplies.ToList() }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.CommentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); IEnumerable<Comment> newCommentReplies = null; commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>())) .Callback((Comment c) => newCommentReplies = c.Comment1); // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); // Arrange - mock commentValidation this._commentValidationMock.Setup(v => v.ValidateCommentText(commentText)) .Returns(validatedCommentText); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act Comment reply = target.Reply(comment.CommentId, user.UserId, commentText); // Assert Assert.IsNotNull(reply); Assert.AreEqual(user.UserId, reply.UserId); Assert.AreEqual(comment.ArticleId, reply.ArticleId); Assert.AreEqual(validatedCommentText, reply.Text); Assert.IsTrue(new DateTime() != reply.Date); Assert.IsNotNull(newCommentReplies); Assert.AreEqual(commentReplies.Count() + 1, newCommentReplies.Count()); Assert.IsTrue(newCommentReplies.Contains(reply)); commentRepositoryMock.Verify( r => r.GetById(comment.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); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once); }
public void Edit_UserIsNotAuthorOfTheComment_ThrowsEditingCommentIsForbiddenException() { // Arrange int userId = 1; string newCommentText = "new_comment's_text"; Comment comment = new Comment { CommentId = 2, UserId = userId + 1 }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.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<EditingCommentIsForbiddenException>(() => target.Edit(comment.CommentId, userId, newCommentText)); commentRepositoryMock.Verify( r => r.GetById(comment.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.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void Edit_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException() { // Arrange int commentId = 1; string newCommentText = "new_comment's_text"; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(commentId, -1, newCommentText)); Assert.Throws<ArgumentOutOfRangeException>(() => target.Edit(commentId, 0, newCommentText)); }
public void Edit_PermittedPeriodForEditingExpired_ThrowsPermittedPeriodForEditingExpiredException() { // Arrange string newCommentText = "new_comment's_text"; Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForEditing + 1)) }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.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<PermittedPeriodForEditingExpiredException>( () => target.Edit(comment.CommentId, comment.UserId, newCommentText)); commentRepositoryMock.Verify( r => r.GetById(comment.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.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void Edit_CommentHasNotBeenEditedYet_EditsTheComment() { // Arrange string newCommentText = "new_comment_text"; string newValidatedCommentText = "new_validated_comment_text"; Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.CommentEdit }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.CommentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); CommentEdit commentEdit = null; commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>())) .Callback((Comment c) => commentEdit = c.CommentEdit); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); // Arrange - mock commentValidation this._commentValidationMock.Setup(v => v.ValidateCommentText(newCommentText)) .Returns(newValidatedCommentText); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act target.Edit(comment.CommentId, comment.UserId, newCommentText); // Assert Assert.IsNotNull(commentEdit); Assert.IsTrue(new DateTime() != commentEdit.EditDate); commentRepositoryMock.Verify( r => r.GetById(comment.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 && c.Text == newValidatedCommentText)), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); this._commentValidationMock.Verify(v => v.ValidateCommentText(newCommentText), Times.Once); }
public void Delete_UserIsNotAuthorOfTheComment_ThrowsDeletingCommentIsForbiddenException() { // Arrange int userId = 1; Comment comment = new Comment { CommentId = 2, UserId = userId + 1, Date = DateTime.Now }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup(r => r.GetById(comment.CommentId)) .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<DeletingCommentIsForbiddenException>(() => target.Delete(comment.CommentId, userId)); commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId)), Times.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void CanEdit_UserCanEditTheComment_ReturnsTrue() { // Arrange Comment comment = new Comment { UserId = 1, Date = DateTime.Now }; // Arrange - create target ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanEdit(comment.UserId, comment); //Assert Assert.IsTrue(result); }
public void Reply_CommentTreeLevelIsEqualToMaxCommentTreeLevel_ThrowsMaxCommentTreeLevelReachedException() { // Arrange int userId = 1; string commentText = "comment_text"; Comment comment = new Comment { CommentId = 2, TreeLevel = (byte)this._serviceSettings.MaxCommentTreeLevel }; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.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<MaxCommentTreeLevelReachedException>(() => target.Reply(comment.CommentId, userId, commentText)); commentRepositoryMock.Verify( r => r.GetById(comment.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.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void CanEdit_UserIsNotAuthorOfTheComment_ReturnsFalse() { // Arrange int userId = 1; Comment comment = new Comment { UserId = userId + 1 }; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanEdit(userId, comment); //Assert Assert.IsFalse(result); }
public void Reply_NonexistentUserId_ThrowsUserNotFoundException() { // Arrange Comment comment = new Comment { CommentId = 1 }; int userId = 2; string commentText = "comment_text"; Expression<Func<Comment, object>>[] propertiesToInclude = { c => c.Comment1 }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup( r => r.GetById(comment.CommentId, It.Is<Expression<Func<Comment, object>>[]>( selector => ExpressionHelper.AreExpressionArraysEqual(selector, propertiesToInclude)))) .Returns(comment); // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(userId)) .Returns((User)null); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); // Arrange - Reply target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<UserNotFoundException>(() => target.Reply(comment.CommentId, userId, commentText)); commentRepositoryMock.Verify( r => r.GetById(comment.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.Never); userRepositoryMock.Verify(r => r.GetById(userId), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }
public void CanHaveReply_CommentTreeLevelIsEqualToMaxCommentTreeLevel_ReturnsFalse() { // Arrange Comment comment = new Comment { TreeLevel = (byte)this._serviceSettings.MaxCommentTreeLevel }; ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanHaveReply(comment); // Assert Assert.IsFalse(result); }
public void AddVote_UserHasVotedWithAnotherVoteType_EditsVote() { // Arrange int commentId = 1; int userId = 1; bool voteIsPositive = true; CommentVote[] commentVotes = { new CommentVote { CommentId = commentId, UserId = userId, IsPositive = !voteIsPositive }, 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 == 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.AddVote(commentId, userId, voteIsPositive); // Assert Assert.IsNotNull(newCommentVotes); Assert.AreEqual(commentVotes.Count(), newCommentVotes.Count()); CommentVote commentVote = newCommentVotes.FirstOrDefault( v => v.CommentId == commentId && v.UserId == userId && v.IsPositive == voteIsPositive); Assert.IsNotNull(commentVote); 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.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); }
public void Create_AllCredentialsAreValid_CreatesAndReturnsComment() { // Arrange User user = new User { UserId = 1 }; Article article = new Article { ArticleId = 2 }; string commentText = "comment_text"; string validatedCommentText = "validated_comment_text"; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(user.UserId)) .Returns(user); // Arrange - mock articleRepository Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>(); articleRepositoryMock.Setup(r => r.GetById(article.ArticleId)) .Returns(article); // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.ArticleRepository) .Returns(articleRepositoryMock.Object); unitOfWorkMock.SetupGet(u => u.CommentRepository) .Returns(commentRepositoryMock.Object); // Arrange - mock commentValidation this._commentValidationMock.Setup(v => v.ValidateCommentText(commentText)) .Returns(validatedCommentText); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act Comment comment = target.Create(user.UserId, article.ArticleId, commentText); // Assert Assert.IsNotNull(comment); Assert.AreEqual(user.UserId, comment.UserId); Assert.AreEqual(article.ArticleId, comment.ArticleId); Assert.AreEqual(validatedCommentText, comment.Text); Assert.IsTrue(new DateTime() != comment.Date); userRepositoryMock.Verify(r => r.GetById(user.UserId), Times.Once); articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once); commentRepositoryMock.Verify( r => r.Insert( It.Is<Comment>(c => c.UserId == user.UserId && c.ArticleId == article.ArticleId && c.Text == validatedCommentText)), Times.Once); unitOfWorkMock.Verify(u => u.Save(), Times.Once); this._commentValidationMock.Verify(v => v.ValidateCommentText(commentText), Times.Once); }
public void AddVote_UserHasVotedWithTheSameVoteType_DoNotUpdateTheVote() { // Arrange int commentId = 1; int userId = 1; bool voteIsPositive = true; Comment comment = new Comment { CommentId = commentId, CommentVotes = { new CommentVote { CommentId = commentId, UserId = userId, IsPositive = voteIsPositive } } }; 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 target.AddVote(commentId, userId, voteIsPositive); // Assert 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 Create_NonexistentUserId_ThrowsUserNotFoundException() { // Arrange int userId = 1; int articleId = 2; string commentText = "comment_text"; // Arrange - mock userRepository Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>(); userRepositoryMock.Setup(r => r.GetById(userId)) .Returns((User)null); // Arrange - mock unitOfWork Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>(); unitOfWorkMock.SetupGet(u => u.UserRepository) .Returns(userRepositoryMock.Object); // Arrange - create target ICommentService target = new CommentService(unitOfWorkMock.Object, this._commentValidationMock.Object, this._serviceSettings); // Act and Assert Assert.Throws<UserNotFoundException>(() => target.Create(userId, articleId, commentText)); userRepositoryMock.Verify(r => r.GetById(userId), Times.Once); }
public void CanDelete_CommentIsDeleted_ReturnsFalse() { // Arrange Comment comment = new Comment { UserId = 1, Date = DateTime.Now, IsDeleted = true }; // Arrange - create target ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanDelete(comment.UserId, comment); //Assert Assert.IsFalse(result); }
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); }
public void CanEdit_PermittedPeriodForEditingExpired_ReturnsFalse() { // Arrange Comment comment = new Comment { UserId = 1, Date = DateTime.Now.AddSeconds(-(this._serviceSettings.PermittedPeriodForEditing + 1)) }; // Arrange - create target ICommentService target = new CommentService(new Mock<IUnitOfWork>().Object, this._commentValidationMock.Object, this._serviceSettings); // Act bool result = target.CanEdit(comment.UserId, comment); //Assert Assert.IsFalse(result); }
public void Delete_AllCredentialsAreValid_DeletesTheComment() { // Arrange Comment comment = new Comment { CommentId = 1, UserId = 2, Date = DateTime.Now }; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup(r => r.GetById(comment.CommentId)) .Returns(comment); Comment deletedComment = null; commentRepositoryMock.Setup(r => r.Update(It.IsAny<Comment>())) .Callback((Comment c) => deletedComment = c); // 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.Delete(comment.CommentId, comment.UserId); // Assert Assert.IsNotNull(deletedComment); Assert.IsTrue(deletedComment.IsDeleted); commentRepositoryMock.Verify(r => r.GetById(comment.CommentId), Times.Once); commentRepositoryMock.Verify( r => r.Update(It.Is<Comment>(c => c.CommentId == comment.CommentId && c.UserId == comment.UserId)), Times.Once); unitOfWorkMock.Verify(r => r.Save(), Times.Once); }
public void Delete_NonexistentCommentId_ThrowsCommentNotFoundException() { // Arrange int commentId = 1; int userId = 1; // Arrange - mock commentRepository Mock<ICommentRepository> commentRepositoryMock = new Mock<ICommentRepository>(); commentRepositoryMock.Setup(r => r.GetById(commentId)) .Returns((Comment)null); // 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<CommentNotFoundException>(() => target.Delete(commentId, userId)); commentRepositoryMock.Verify(r => r.GetById(commentId), Times.Once); commentRepositoryMock.Verify(r => r.Update(It.Is<Comment>(c => c.CommentId == commentId)), Times.Never); unitOfWorkMock.Verify(r => r.Save(), Times.Never); }