예제 #1
0
        public void SaveEditedArticleTest(
            [Frozen] Mock <IUnitOfWork> unitOfWork,
            [Frozen] Mock <IRepository <Article> > repo,
            IMapper mapper,
            FrontModel.Article articlevm,
            HomeController sut)
        {
            // arrange:
            var article = mapper.Map <Article>(articlevm);

            unitOfWork.Setup(x => x.ArticlesRepository).Returns(repo.Object);
            repo.Setup(x => x.FindById(It.IsAny <int>())).Returns(article);

            // act:
            var result = sut.EditArticle(articlevm);

            // assert:
            Assert.True(articlevm.Id > 0);
            unitOfWork.Verify(x => x.ArticlesRepository, Times.Once());
            repo.Verify(x => x.Attach(It.IsAny <Article>()), Times.Once(), "Attach method not invoked.");
            repo.Verify(x => x.Update(It.IsAny <Article>()), Times.Once(), "Update method not invoked.");
            unitOfWork.Verify(x => x.SaveChanges(), Times.Once(), "SaveChanges method not invoked.");

            Assert.True(result.RouteValues.Count() > 0);
            Assert.Equal("Index", result.RouteValues["action"]);
        }
예제 #2
0
        public void SaveAddedArticleTest(
            [Frozen] Mock <IRepository <Article> > repo,
            [Frozen] Mock <IUnitOfWork> unitOfWork,
            FrontModel.Article article,
            HomeController sut)
        {
            // arrange:
            article.Id = 0;
            unitOfWork.Setup(x => x.ArticlesRepository).Returns(repo.Object);

            // act:
            var result = sut.AddArticle(article);

            // assert:
            unitOfWork.Verify(x => x.ArticlesRepository, Times.Once(), "ArticlesRepository not invoked.");
            repo.Verify(x => x.Add(It.IsAny <Article>()), Times.Once(), "Add method not invoked.");
            unitOfWork.Verify(x => x.SaveChanges(), Times.Once(), "SaveChanges method not invoked.");

            Assert.True(result.RouteValues.Count() > 0);
            Assert.Equal("Index", result.RouteValues["action"]);
        }