コード例 #1
0
        public void Should_Return_Error_2_Update_Data()
        {
            Mock <IBuyerFacade> mockBuyerFacade = new Mock <IBuyerFacade>();

            mockBuyerFacade.Setup(p => p.UpdateById(It.IsAny <Buyer>())).Returns(0);
            mockBuyerFacade.Setup(p => p.ReadById(It.IsAny <int>())).Returns(buyerDataUtil.GetModelData());

            Mock <IServiceProvider> mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(p => p.GetService(typeof(IBuyerFacade))).Returns(mockBuyerFacade.Object);
            mockServiceProvider.Setup(p => p.GetService(typeof(ProjectDbContext))).Returns(MemoryDbHelper.GetDB(Helper.GetCurrentMethod(ENTITY)));

            Mock <IMapper> mockMapper = new Mock <IMapper>();

            mockMapper.Setup(p => p.Map <Buyer>(It.IsAny <BuyerViewModel>()))
            .Returns(buyerDataUtil.GetModelData());

            BuyersController buyersController = new BuyersController(mockServiceProvider.Object, mockMapper.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                }
            };

            buyersController.ControllerContext.HttpContext.Request.Path = new PathString("/v1/unit-test");

            BuyerViewModel viewModel = buyerDataUtil.GetViewModelData();
            IActionResult  result    = buyersController.Put(1, viewModel);

            Assert.Equal(HttpStatusCode.InternalServerError, (HttpStatusCode)((ObjectResult)result).StatusCode);
        }
コード例 #2
0
        public void Should_Return_Not_Found_Update_Data()
        {
            ProjectDbContext    projectDbContext = MemoryDbHelper.GetDB(Helper.GetCurrentMethod(ENTITY));
            Mock <IBuyerFacade> mockBuyerFacade  = new Mock <IBuyerFacade>();

            mockBuyerFacade.Setup(p => p.UpdateById(It.IsAny <Buyer>())).Returns(0);

            Mock <IServiceProvider> mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(p => p.GetService(typeof(IBuyerFacade))).Returns(mockBuyerFacade.Object);
            mockServiceProvider.Setup(p => p.GetService(typeof(ProjectDbContext))).Returns(projectDbContext);

            Mock <IMapper> mockMapper = new Mock <IMapper>();

            mockMapper.Setup(p => p.Map <Buyer>(It.IsAny <BuyerViewModel>()))
            .Returns(buyerDataUtil.GetModelData());

            BuyersController buyersController = new BuyersController(mockServiceProvider.Object, mockMapper.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                }
            };

            buyersController.ControllerContext.HttpContext.Request.Path = new PathString("/v1/unit-test");

            //Add data for make it double
            projectDbContext.Add(buyerDataUtil.GetModelData());
            projectDbContext.SaveChanges();

            BuyerViewModel viewModel = buyerDataUtil.GetViewModelData();
            IActionResult  result    = buyersController.Put(1, viewModel);

            Assert.Equal(HttpStatusCode.NotFound, (HttpStatusCode)((NotFoundObjectResult)result).StatusCode);
        }