コード例 #1
0
        public void GetDiscount(string productName)
        {
            _repository.Setup(p => p.GetDiscount(productName)).ReturnsAsync(_coupon);
            var coupon = _controller.GetDiscount(productName);

            if (coupon.Result.Result is OkObjectResult okResult)
            {
                Assert.AreEqual((int)HttpStatusCode.OK, okResult.StatusCode);
            }
            else
            {
                Assert.Fail();
            }
        }
コード例 #2
0
        public void GetDiscountTest_WhenUserValid()
        {
            // Arrange
            Mock <IDiscountService> discountMock = new Mock <IDiscountService>();

            discountMock.Setup(x => x.GetDiscountByUserRole(It.IsAny <Guid>())).Returns(2);

            DiscountController discountController = new DiscountController(discountMock.Object);
            List <Claim>       claims             = new List <Claim>();

            claims.Add(new Claim(ClaimTypes.Role, Guid.NewGuid().ToString()));
            claims.Add(new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()));
            var mockContext = new Mock <HttpContext>(MockBehavior.Strict);

            mockContext.SetupGet(h => h.User.Claims).Returns(claims);
            discountController.ControllerContext = new ControllerContext()
            {
                HttpContext = mockContext.Object
            };

            //Act
            var discount = discountController.GetDiscount();

            // Assert
            Assert.NotNull(discount);
            Assert.IsType <OkObjectResult>(discount);
        }
コード例 #3
0
        public void GetDiscountReturnsBadRequestResultWhenIdIsNull()
        {
            var mock       = new Mock <IDiscountRepository>();
            var controller = new DiscountController(mock.Object);


            var result = controller.GetDiscount(null);


            Xunit.Assert.IsType <BadRequestResult>(result);
        }
コード例 #4
0
        public void GetDiscountTest_WhenUserNotValid()
        {
            // Arrange
            Mock <IDiscountService> discountMock = new Mock <IDiscountService>();

            discountMock.Setup(x => x.GetDiscountByUserRole(It.IsAny <Guid>())).Returns(2);

            DiscountController discountController = new DiscountController(discountMock.Object);

            //Act and Assert
            Assert.Throws <NullReferenceException>(() => discountController.GetDiscount());
        }
コード例 #5
0
        public async Task ReturnsValidDiscountCoupon()
        {
            //Arrange
            var expectedCoupon = this.DesiredDiscount;

            _mockDiscountRepository.Setup(x => x.GetDiscount(It.IsAny <string>())).ReturnsAsync(expectedCoupon);

            //Act
            var result = await _sut.GetDiscount("IPhone X");

            //Assert
            Assert.NotNull(result);
            var response = Assert.IsType <ActionResult <Coupon> >(result);
            var okResult = response.Result as OkObjectResult;

            Assert.Equal(200, okResult.StatusCode.Value);

            var returnedCoupon = Assert.IsType <Coupon>(okResult.Value);

            Assert.Equal(expectedCoupon.ProductName, returnedCoupon.ProductName);
            Assert.Equal(expectedCoupon.Amount, returnedCoupon.Amount);
        }
コード例 #6
0
        public void GetDiscountReturnsNotFoundResultWhenDiscountNotFound()
        {
            int testDiscountId = 1;
            var mock           = new Mock <IDiscountRepository>();

            mock.Setup(repo => repo.Get(testDiscountId))
            .Returns(null as Discount);
            var controller = new DiscountController(mock.Object);

            var result = controller.GetDiscount(testDiscountId);

            Xunit.Assert.IsType <NotFoundResult>(result);
        }
コード例 #7
0
        public void GetDiscountReturnsViewResultWithDiscount()
        {
            int testDiscountId = 1;
            var mock           = new Mock <IDiscountRepository>();

            mock.Setup(repo => repo.Get(testDiscountId))
            .Returns(GetTestDiscounts().FirstOrDefault(p => p.Id == testDiscountId));
            var controller = new DiscountController(mock.Object);


            var result = controller.GetDiscount(testDiscountId);


            var viewResult = Xunit.Assert.IsType <ViewResult>(result);
            var model      = Xunit.Assert.IsType <Discount>(viewResult.ViewData.Model);

            Xunit.Assert.Equal("1", model.UserId);
            Xunit.Assert.Equal("20", model.SizeOfDiscount);
            Xunit.Assert.Equal(testDiscountId, model.Id);
        }
コード例 #8
0
        private void confrim_Click(object sender, EventArgs e)
        {
            string FoodId         = tbId.Text;
            var    DiscountResult = DiscountController.DeleteDiscount(FoodId);

            if (DiscountResult)
            {
                MessageBox.Show("Remove Discount Successfully");
                var foodResult      = FoodController.GetFood(FoodId);
                var DiscountResult1 = DiscountController.GetDiscount(FoodId);
                int a           = foodResult.Price + DiscountResult1.Amount;
                var updatePrice = new
                {
                    Id    = FoodId,
                    Price = a
                };
                var updateResult = FoodController.UpdateFood2(updatePrice);
            }
            else
            {
                MessageBox.Show("Food Not Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }