Beispiel #1
0
        public async Task Post_ValidObject_ReturnsOkResult()
        {
            //Arrange
            Mock <ICartItemService> moqRepo = new Mock <ICartItemService>();//Mock is type of our Interface

            Category the_Category_Test_Data = new Category()
            {
                CategoryId   = 2,
                CategoryName = "Items"
            };

            Product product = new Product()
            {
                ProductId   = 3,
                ProductName = "Chocolate",
                Price       = 3,
                CategoryId  = 2,
                Category    = the_Category_Test_Data
            };

            CartItemDTO the_CartItem = new CartItemDTO()
            {
                ProductId = 3,
                Product   = product,
                Price     = 3,
                Quantity  = 2
            };

            //access the function inside the service class and specify what it returns
            moqRepo.Setup(i => i.CreateCartItem(It.IsAny <CartItemDTO>())).Returns(() => Task.FromResult(the_CartItem));
            CartItemController controller = new CartItemController(moqRepo.Object);//pass context and moq object inside controller

            //Act
            ActionResult createdResponse = await controller.Add_To_Cart(the_CartItem);

            //Assert
            var okResult = Assert.IsType <OkObjectResult>(createdResponse);

            Assert.True((okResult.Value as CartItemDTO).ProductId > 0);
        }