Пример #1
0
        public void Delete_UserIsNotTheAuthorOfArticle_ThrowsDeletingArticleIsForbiddenException()
        {
            // Arrange
            int userId = 1;
            Article article = new Article { ArticleId = 2, UserId = userId + 1 };

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            articleRepositoryMock.Setup(r => r.GetById(article.ArticleId))
            .Returns(article);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act and Assert
            Assert.Throws<DeletingArticleIsForbiddenException>(() => target.Delete(article.ArticleId, userId));

            // Assert
            articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once);
            articleRepositoryMock.Verify(r => r.Delete(It.Is<Article>(t => t.ArticleId == article.ArticleId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Пример #2
0
        public void Delete_NonexistentArticleId_ThrowsArticleNotFoundException()
        {
            // Arrange
            int articleId = 1;
            int userId = 2;

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            articleRepositoryMock.Setup(r => r.GetById(articleId))
            .Returns((Article)null);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArticleNotFoundException>(() => target.Delete(articleId, userId));

            articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once);
            articleRepositoryMock.Verify(r => r.Delete(It.Is<Article>(t => t.ArticleId == articleId)), Times.Never);

            unitOfWorkMock.Verify(r => r.Save(), Times.Never);
        }
Пример #3
0
        public void Delete_UserIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            int articleId = 1;

            IArticleService target = new ArticleService(new Mock<IUnitOfWork>().Object, this._articleValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Delete(articleId, -1));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.Delete(articleId, 0));
        }
Пример #4
0
        public void Delete_ArticleIdIsValid_DeletesArticle()
        {
            // Arrange
            Article article = new Article { ArticleId = 1, UserId = 2 };

            // Arrange - mock articleRepository
            Mock<IArticleRepository> articleRepositoryMock = new Mock<IArticleRepository>();

            articleRepositoryMock.Setup(r => r.GetById(article.ArticleId))
            .Returns(article);

            // Arrange - mock unitOfWork
            Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();

            unitOfWorkMock.SetupGet(u => u.ArticleRepository)
            .Returns(articleRepositoryMock.Object);

            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            target.Delete(article.ArticleId, article.UserId);

            // Assert
            articleRepositoryMock.Verify(r => r.GetById(article.ArticleId), Times.Once);
            articleRepositoryMock.Verify(r => r.Delete(It.Is<Article>(t => t.ArticleId == article.ArticleId)), Times.Once);

            unitOfWorkMock.Verify(r => r.Save(), Times.Once);
        }