Ejemplo n.º 1
0
        public async void Index_Success()
        {
            // Arrange
            var articleAdapterMock = new Mock <IArticleAdapter>();

            articleAdapterMock
            .Setup(aa => aa.GetArticlesByPage(1, 20))
            .Returns(Task.FromResult(new FilteredModels <Article>
            {
                Items = new List <Article>
                {
                    new Article {
                        Id = 1, Text = "Test1", CommentsCount = 0
                    },
                    new Article {
                        Id = 2, Text = "Test2", CommentsCount = 0
                    }
                }
            }));

            var controller = new ArticlesController(articleAdapterMock.Object);

            // Act
            var actual = await controller.Index(1);

            // Assert
            var view  = Assert.IsType <ViewResult>(actual);
            var model = Assert.IsType <ArticlesViewModel>(view.Model);

            Assert.Null(model.Error);
            Assert.Equal(2, model.Articles.Items.Count);
        }
        public async Task IndexReturnsAViewResultWithAListOfArticlesByTitle()
        {
            string search   = "ok";
            var    mockUnit = new Mock <IUnitOfWork>();

            mockUnit.Setup(u => u.ArticlesRepository);
            var mockServ = new Mock <IArticleService>();

            mockServ.Setup(u => u.GetArticleByTitle(It.IsAny <string>())).Returns(GetTestUsersByTitleAsync(search));
            //  ArticleService articleService = new ArticleService(mockUnit.Object);


            using (var context = new ApplicationDbContext(options))
            {
                var http       = new Mock <IHttpContextAccessor>();
                var controller = new ArticlesController(mockServ.Object, context, http.Object);

                // Act
                var result = controller.Index(search);

                // Assert
                var viewResult = Assert.IsType <ViewResult>(result.Result);
                var model      = Assert.IsAssignableFrom <Task <IEnumerable <Article> > >(viewResult.Model);
                //  var model =await  Assert.IsType< Task<IEnumerable<Article>>>((Task<IEnumerable<Article>>)viewResult.ViewData.Model);
                Assert.Equal(GetTestUsersByTitleAsync(search).Result.Count(), model.Result.Count());
            }
        }
        public async Task Index_ReturnsAllArticles()
        {
            // Arrange
            var articleModel = new ArticleConciseViewModel()
            {
                Id = 1, Title = "First"
            };
            bool methodCalled = false;

            var mockRepository = new Mock <IAdminArticlesService>();

            mockRepository.Setup(s => s.GetAllArticlesAsync())
            .ReturnsAsync(new[] { articleModel })
            .Callback(() => methodCalled = true);

            var userRepository = new Mock <IAdminUsersService>();

            userRepository.Setup(repo => repo.GetAuthorsAsync())
            .Callback(() => methodCalled = true);

            var controller = new ArticlesController(userRepository.Object, mockRepository.Object);

            // Act
            var result = await controller.Index();

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            var resultView = result as ViewResult;

            Assert.IsNotNull(resultView.Model);
            Assert.IsInstanceOfType(resultView.Model, typeof(IEnumerable <ArticleConciseViewModel>));
            Assert.IsTrue(methodCalled);
        }
Ejemplo n.º 4
0
        public async void Index_Articles_NotFound()
        {
            // Arrange
            var articleAdapterMock = new Mock <IArticleAdapter>();

            articleAdapterMock
            .Setup(aa => aa.GetArticlesByPage(1, 20))
            .Throws(new AdapterException(StatusCodes.Status404NotFound, "На данной странице нет статей"));

            var controller = new ArticlesController(articleAdapterMock.Object);

            // Act
            var actual = await controller.Index(1);

            // Assert
            var view  = Assert.IsType <ViewResult>(actual);
            var model = Assert.IsType <ArticlesViewModel>(view.Model);

            Assert.Equal("На данной странице нет статей", model.Error.Text);
        }
Ejemplo n.º 5
0
        public void CallGetPageMethod_WhenParamsAreValid()
        {
            // Arrange
            var articleServiceMock = new Mock <IArticleService>();
            var mappingServiceMock = new Mock <IMappingService>();
            var storeServiceMock   = new Mock <IStoreService>();
            var userServiceMock    = new Mock <IUserService>();
            var utilsMock          = new Mock <IUtilitiesService>();

            var controller = new ArticlesController(articleServiceMock.Object, mappingServiceMock.Object, storeServiceMock.Object, userServiceMock.Object, utilsMock.Object);

            var articleMock       = new Article();
            var mappedArticleMock = new ArticleItemViewModel();
            var modelMock         = new ArticlesListViewModel();

            var articlesMock = new List <Article>()
            {
                articleMock
            };
            var mappedArticlesMock = new List <ArticleItemViewModel>()
            {
                mappedArticleMock
            };

            articleServiceMock.Setup(x => x.GetPagesCount(It.IsAny <string>())).Returns(1);
            utilsMock.Setup(x => x.GetPage(It.IsAny <int?>(), It.IsAny <int>())).Returns(1);
            articleServiceMock.Setup(x => x.GetFilteredArticlesByPage(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())).Returns(articlesMock);
            mappingServiceMock.Setup(x => x.Map <IEnumerable <ArticleItemViewModel> >(It.IsAny <IEnumerable <Article> >())).Returns(mappedArticlesMock);
            mappingServiceMock.Setup(x => x.Map <ArticlesListViewModel>(It.IsAny <IEnumerable <ArticleItemViewModel> >())).Returns(modelMock);
            utilsMock.Setup(x => x.AssignViewParams <ArticlesListViewModel>(It.IsAny <ArticlesListViewModel>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(modelMock);

            // Act
            controller.Index(null, null);

            // Assert
            utilsMock.Verify(x => x.GetPage(It.IsAny <int?>(), It.IsAny <int>()), Times.Once);
        }
Ejemplo n.º 6
0
        public async Task IndexTest_ReturnsViewWithArticlesList()
        {
            // Arrange
            var mockArticlesList = new List <Article>
            {
                new Article {
                    Title = "mock article 1"
                },
                new Article {
                    Title = "mock article 2"
                }
            };

            articlesRepoMock.Setup(repo => repo.GetAll()).Returns(Task.FromResult(mockArticlesList));

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <Article> >(viewResult.ViewData.Model);

            Assert.Equal(2, model.Count());
        }