Пример #1
0
        public void GetById_ArticleIdIsValid_ReturnsArticle()
        {
            // Arrange
            Article testArticle = new Article { ArticleId = 1 };

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

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

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

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

            // Arrange - create target
            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            Article article = target.GetById(testArticle.ArticleId);

            // Assert
            Assert.AreSame(testArticle, article);

            articleRepositoryMock.Verify(r => r.GetById(testArticle.ArticleId), Times.Once);
        }
Пример #2
0
        public void GetById_NonexistentArticleId_ReturnsNull()
        {
            // Arrange
            int articleId = 1;

            // 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);

            // Arrange - create target
            IArticleService target = new ArticleService(unitOfWorkMock.Object, this._articleValidationMock.Object);

            // Act
            Article article = target.GetById(articleId);

            // Assert
            Assert.IsNull(article);

            articleRepositoryMock.Verify(r => r.GetById(articleId), Times.Once);
        }
Пример #3
0
        public void GetById_ArticleIdIsLessOrEqualToZero_ThrowsArgumentOutOfRangeException()
        {
            // Arrange
            IArticleService target = new ArticleService(new Mock<IUnitOfWork>().Object, this._articleValidationMock.Object);

            // Act and Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => target.GetById(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => target.GetById(0));
        }