public void NavMenu_Shows_Home_Link_At_Top()
        {
            // Arrange: Given any repository
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository();

            // Act: ... when we render the navigation menu
            var result = new NavController(mockProductsRepository).Menu(null);

            // Assert: ... then the top link is to Home
            var topLink = ((IEnumerable<NavLink>)result.ViewData.Model).First();
            topLink.RouteValues["controller"].ShouldEqual("Products");
            topLink.RouteValues["action"].ShouldEqual("List");
            topLink.RouteValues["page"].ShouldEqual(1);
            topLink.RouteValues["category"].ShouldEqual(null);
            topLink.Text.ShouldEqual("Home");
        }
        public void NavMenu_Highlights_Current_Category()
        {
            // Arrange: Given two categories...
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { Category = "A", Name = "ProductA" },
                new Product { Category = "B", Name = "ProductB" }
            );

            // Act: ... when we render the navigation menu
            var result = new NavController(mockProductsRepository).Menu("B");

            // Assert: ... then only the current category is highlighted
            var highlightedLinks = ((IEnumerable<NavLink>)result.ViewData.Model)
                                    .Where(x => x.IsSelected).ToList();
            highlightedLinks.Count.ShouldEqual(1);
            highlightedLinks[0].Text.ShouldEqual("B");
        }
        public void NavMenuIncludesAlphabeticalListOfDistinctCategories()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { Category = "Vegetable", Name = "ProductA" },
                new Product { Category = "Animal", Name = "ProductB" },
                new Product { Category = "Vegetable", Name = "ProductC" },
                new Product { Category = "Mineral", Name = "ProductD" }
            );

            var controller = new NavController(mockProductsRepository);
            var result = controller.Menu(null);

            var categoryLinks = ((IEnumerable<NavLink>)result.ViewData.Model)
                                .Where(x => x.RouteValues["category"] != null);

            CollectionAssert.AreEqual(new[] { "Animal", "Mineral", "Vegetable" }, categoryLinks.Select(x => x.RouteValues["category"]));

            foreach (var link in categoryLinks) {
                link.RouteValues["controller"].ShouldEqual("Products");
                link.RouteValues["action"].ShouldEqual("List");
                link.RouteValues["page"].ShouldEqual(1);
                link.Text.ShouldEqual(link.RouteValues["category"]);
            }
        }
Esempio n. 4
0
 public void Can_Create_Categories()
 {
     // Arrange
     // - create the mock repository
     Mock<IProductRepository> mock = new Mock<IProductRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1", Category = "Apples"},
         new Product {ProductID = 2, Name = "P2", Category = "Apples"},
         new Product {ProductID = 3, Name = "P3", Category = "Plums"},
         new Product {ProductID = 4, Name = "P4", Category = "Oranges"},
         });
     // Arrange - create the controller
     NavController target = new NavController(mock.Object);
     // Act = get the set of categories
     string[] results = ((IEnumerable<string>)target.Menu().Model).ToArray();
     // Assert
     Assert.AreEqual(results.Length, 3);
     Assert.AreEqual(results[0], "Apples");
     Assert.AreEqual(results[1], "Oranges");
     Assert.AreEqual(results[2], "Plums");
 }