예제 #1
0
        public void SearchResults_WithWrongCategoryId_MustThrowHttpException()
        {
            //Mock articles repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock categories repository
            int categoryId       = 50;
            var mockedCategories = new Mock <IRepository <Category> >();

            mockedCategories.Setup(repo => repo.GetById(50)).Returns(_categories.FirstOrDefault(cat => cat.Id == categoryId));

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Categories).Returns(() => { return(mockedCategories.Object); });
            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);
            var searchModel = new SearchArticlesViewModel
            {
                CategoryId = categoryId
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
        }
예제 #2
0
        public void SearchResults_WithFromPriceBiggerThanToPrice_MustReturnTheSameSearchModel()
        {
            //Mock repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);
            var searchModel = new SearchArticlesViewModel
            {
                FromPrice = 50,
                ToPrice   = 20
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
            var model   = results.Model as SearchArticlesViewModel;

            Assert.AreSame(model, searchModel);
        }
예제 #3
0
        public void SearchResults_OnlySearchSubstring_MustReturnOnlyArticlesWhoseNameForTheCurrentCultureContainsThisSubstring()
        {
            //Mock repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);

            articlesController.CurrentCulture = Culture.bg.ToString();
            var searchModel = new SearchArticlesViewModel
            {
                SearchSubstring = "странно"
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
            var model   = results.Model as IList <ArticleViewModel>;

            Assert.AreEqual(2, model.Count);
            foreach (var article in model)
            {
                Assert.IsTrue(article.Name.Contains(searchModel.SearchSubstring));
            }
        }
예제 #4
0
        public void SearchResults_OnlyToPRiceTwenty_MustReturnOnlyArticlesWithPriceLowerThanTwenty()
        {
            //Mock repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);
            var searchModel = new SearchArticlesViewModel
            {
                ToPrice = 20
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
            var model   = results.Model as IList <ArticleViewModel>;

            Assert.AreEqual(3, model.Count);
            foreach (var article in model)
            {
                Assert.IsTrue(article.Price <= searchModel.ToPrice);
            }
        }
예제 #5
0
        public void SearchResults_OnlyCategoryId_MustReturnOnlyArticlesFromThisCategory()
        {
            //Mock repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);
            var searchModel = new SearchArticlesViewModel
            {
                CategoryId = 1
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
            var model   = results.Model as IList <ArticleViewModel>;

            Assert.AreEqual(2, model.Count);
            Assert.AreEqual(1, model[0].Id);
            Assert.AreEqual(4, model[1].Id);
        }
예제 #6
0
        public void SearchResults_WithFullSearchModel_MustReturnOnlyArticlesThatMatchAllTheCriterias()
        {
            //Mock repository
            var mockedArticles = new Mock <IRepository <Article> >();

            mockedArticles.Setup(repo => repo.All()).Returns(_articles.AsQueryable());

            //Mock data
            var mockedUow = new Mock <IUowData>();

            mockedUow.Setup(uow => uow.Articles).Returns(() => { return(mockedArticles.Object); });

            // Act
            ArticlesController articlesController = new ArticlesController(mockedUow.Object);

            articlesController.CurrentCulture = Culture.bg.ToString();
            var searchModel = new SearchArticlesViewModel
            {
                SearchSubstring = "странно",
                CategoryId      = 2,
                FromPrice       = 20,
                ToPrice         = 30,
            };

            var results = articlesController.SearchResults(searchModel) as ViewResult;
            var model   = results.Model as IList <ArticleViewModel>;

            Assert.AreEqual(1, model.Count);

            Assert.IsTrue(model[0].Name.Contains(searchModel.SearchSubstring));
            Assert.IsTrue(model[0].Price >= searchModel.FromPrice);
            Assert.IsTrue(model[0].Price <= searchModel.ToPrice);
            Assert.IsTrue(model[0].Price <= searchModel.ToPrice);

            //We dont have property for category id in the view model, so we check if the id of the result is
            //one of the ids of the two articles that we know are from the category we search for
            Assert.IsTrue(model[0].Id == _articles[1].Id || model[0].Id == _articles[2].Id);
        }