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

            moqRepo.Setup(repo => repo.GetCartItem(1)).Returns(MyCartItemList().FirstOrDefault(i => i.ProductId == 1)); //access the function inside the service class and specify what it returns
            CartItemController controller = new CartItemController(moqRepo.Object);                                     //pass moq object inside controller

            //Act
            ActionResult <CartItemDTO> okResult = controller.GetById(1);//1 is valid Id

            //Assert
            Assert.IsType <OkObjectResult>(okResult.Result);//When Id is valid the result is type of OkObjectResult
        }
Beispiel #2
0
        public void GetById_InvalidIdPassed_ReturnsNotFoundResult()
        {
            //Arrange
            Mock <ICartItemService> moqRepo = new Mock <ICartItemService>();                                              //Mock is type of our Interface

            moqRepo.Setup(repo => repo.GetCartItem(-1)).Returns(MyCartItemList().FirstOrDefault(i => i.ProductId == -1)); //access the function inside the service class and specify what it returns
            CartItemController controller = new CartItemController(moqRepo.Object);                                       //pass moq object inside controller

            //Act
            ActionResult <CartItemDTO> not_Found_Result = controller.GetById(-1);// -1 is Invalid Id

            //Assert
            Assert.IsType <NotFoundResult>(not_Found_Result.Result);
        }
Beispiel #3
0
        public void GetById_ExistingIntIdPassed_ReturnsRightItem()
        {
            //Arrange
            Mock <ICartItemService> moqRepo = new Mock <ICartItemService>();                                            //Mock is type of our Interface

            moqRepo.Setup(repo => repo.GetCartItem(1)).Returns(MyCartItemList().FirstOrDefault(i => i.ProductId == 1)); //access the function inside the service class and specify what it returns
            CartItemController controller = new CartItemController(moqRepo.Object);                                     //pass moq object inside controller

            //Act
            OkObjectResult okResult = controller.GetById(1).Result as OkObjectResult;

            //Assert
            Assert.Equal(5, (okResult.Value as CartItemDTO).Quantity);
        }