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_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 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 Delete_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.Delete(commentId, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => target.Delete(commentId, 0)); }
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); }