public async void Get_Exists() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new ApiProductCategoryResponseModel())); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Get(default(int)); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var record = (response as OkObjectResult).Value as ApiProductCategoryResponseModel; record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Update_Errors() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); var mockResult = new Mock <UpdateResponse <ApiProductCategoryResponseModel> >(); mockResult.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiProductCategoryRequestModel>())).Returns(Task.FromResult <UpdateResponse <ApiProductCategoryResponseModel> >(mockResult.Object)); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new ApiProductCategoryResponseModel())); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiProductCategoryModelMapper()); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Update(default(int), new ApiProductCategoryRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Update(It.IsAny <int>(), It.IsAny <ApiProductCategoryRequestModel>())); }
public async void All_Not_Exists() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult <List <ApiProductCategoryResponseModel> >(new List <ApiProductCategoryResponseModel>())); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.All(1000, 0); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var items = (response as OkObjectResult).Value as List <ApiProductCategoryResponseModel>; items.Should().BeEmpty(); mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>())); }
public async void Patch_Record_Not_Found() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); var mockResult = new Mock <ActionResponse>(); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiProductCategoryResponseModel>(null)); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var patch = new JsonPatchDocument <ApiProductCategoryRequestModel>(); patch.Replace(x => x.ModifiedDate, DateTime.Parse("1/1/1987 12:00:00 AM")); IActionResult response = await controller.Patch(default(int), patch); response.Should().BeOfType <StatusCodeResult>(); (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Create_No_Errors() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); var mockResponse = new CreateResponse <ApiProductCategoryResponseModel>(new FluentValidation.Results.ValidationResult()); mockResponse.SetRecord(new ApiProductCategoryResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiProductCategoryRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiProductCategoryResponseModel> >(mockResponse)); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiProductCategoryRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiProductCategoryResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiProductCategoryRequestModel>())); }
public async void BulkInsert_Errors() { ProductCategoryControllerMockFacade mock = new ProductCategoryControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiProductCategoryResponseModel> >(new FluentValidation.Results.ValidationResult()); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiProductCategoryRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiProductCategoryResponseModel> >(mockResponse.Object)); ProductCategoryController controller = new ProductCategoryController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var records = new List <ApiProductCategoryRequestModel>(); records.Add(new ApiProductCategoryRequestModel()); IActionResult response = await controller.BulkInsert(records); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiProductCategoryRequestModel>())); }
public async Task Get_ItemWithIdDoesExist_ReturnsOkResult_WithCorrectItem() { var item = new ProductCategory() { Id = "123-abc", Name = "TestCategory" }; var repoMock = new Mock <IProductCategories>(); repoMock.Setup(p => p.GetAsync(item.Id)) .Returns(Task.FromResult(item)); var categoryController = new ProductCategoryController(repoMock.Object, null); var response = await categoryController.GetProductCategory(item.Id); Assert.IsType <OkObjectResult>(response.Result); var objectResult = response.Result as OkObjectResult; Assert.IsType <ProductCategoryResult>(objectResult.Value); Assert.Equal(item.Name, (objectResult.Value as ProductCategoryResult).ResultData.FirstOrDefault().Name); }