public void CanAddToCart() { // Arrange - create the mock repository var mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductId = 1, Name = "P1", CategoryId = 1}, }.AsQueryable()); // Arrange - create a Cart var cart = new Cart(); // Arrange - create the controller var target = new CartController(mock.Object, null); // Action - add a product to the cart target.AddToCart(cart, 1, null); // Assert Assert.AreEqual(cart.Lines.Count(), 1); Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1); }
public void CannotAddNewProductFromDifferentEstablishment() { // Arrange - create the mock repository var mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductId = 1, Name = "P1", EstablishmentId = 1}, new Product {ProductId = 2, Name = "P2", EstablishmentId = 2} }.AsQueryable()); // Arrange - create a Cart var cart = new Cart(); // Arrange - create the controller var target = new CartController(mock.Object, null); // Action - add a product to the cart target.AddToCart(cart, 1, null); try { target.AddToCart(cart, 2, null); Assert.Fail(); // If we get to this line we have failed. } catch (Exception) { // If we add a throw; here the test does not run. } // Assert Assert.AreEqual(cart.Lines.Count(), 1); Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1); }
public void AddingProductToCartGoesToCartScreen() { // Arrange - create the mock repository var mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductId = 1, Name = "P1", CategoryId = 1}, }.AsQueryable()); // Arrange - create a Cart var cart = new Cart(); // Arrange - create the controller var target = new CartController(mock.Object, null); // Action - add a product to the cart RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl"); // Assert Assert.AreEqual(result.RouteValues["action"], "Index"); Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl"); }