Пример #1
0
        public async void Patch_No_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();
            var mockResult = new Mock <UpdateResponse <ApiPostResponseModel> >();

            mockResult.SetupGet(x => x.Success).Returns(true);
            mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiPostRequestModel>()))
            .Callback <int, ApiPostRequestModel>(
                (id, model) => model.AcceptedAnswerId.Should().Be(1)
                )
            .Returns(Task.FromResult <UpdateResponse <ApiPostResponseModel> >(mockResult.Object));
            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiPostResponseModel>(new ApiPostResponseModel()));
            PostController controller = new PostController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiPostModelMapper());

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var patch = new JsonPatchDocument <ApiPostRequestModel>();

            patch.Replace(x => x.AcceptedAnswerId, 1);

            IActionResult response = await controller.Patch(default(int), patch);

            response.Should().BeOfType <OkObjectResult>();
            (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK);
            mock.ServiceMock.Verify(x => x.Update(It.IsAny <int>(), It.IsAny <ApiPostRequestModel>()));
        }
Пример #2
0
        public async void BulkInsert_No_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();

            var mockResponse = new CreateResponse <ApiPostResponseModel>(new FluentValidation.Results.ValidationResult());

            mockResponse.SetRecord(new ApiPostResponseModel());
            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiPostRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiPostResponseModel> >(mockResponse));
            PostController controller = new PostController(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 <ApiPostRequestModel>();

            records.Add(new ApiPostRequestModel());
            IActionResult response = await controller.BulkInsert(records);

            response.Should().BeOfType <OkObjectResult>();
            (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK);
            var result = (response as OkObjectResult).Value as List <ApiPostResponseModel>;

            result.Should().NotBeEmpty();
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiPostRequestModel>()));
        }
Пример #3
0
        public async void Get_Not_Exists()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();

            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiPostResponseModel>(null));
            PostController controller = new PostController(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 <StatusCodeResult>();
            (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound);
            mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>()));
        }
Пример #4
0
        public async void Delete_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();
            var mockResult = new Mock <ActionResponse>();

            mockResult.SetupGet(x => x.Success).Returns(false);
            mock.ServiceMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.FromResult <ActionResponse>(mockResult.Object));
            PostController controller = new PostController(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.Delete(default(int));

            response.Should().BeOfType <ObjectResult>();
            (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity);
            mock.ServiceMock.Verify(x => x.Delete(It.IsAny <int>()));
        }
Пример #5
0
        public async void All_Not_Exists()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();

            mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult <List <ApiPostResponseModel> >(new List <ApiPostResponseModel>()));
            PostController controller = new PostController(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 <ApiPostResponseModel>;

            items.Should().BeEmpty();
            mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>()));
        }
Пример #6
0
        public async void Update_NotFound()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();
            var mockResult = new Mock <UpdateResponse <ApiPostResponseModel> >();

            mockResult.SetupGet(x => x.Success).Returns(false);
            mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiPostRequestModel>())).Returns(Task.FromResult <UpdateResponse <ApiPostResponseModel> >(mockResult.Object));
            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiPostResponseModel>(null));
            PostController controller = new PostController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiPostModelMapper());

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Update(default(int), new ApiPostRequestModel());

            response.Should().BeOfType <StatusCodeResult>();
            (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound);
            mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>()));
        }
Пример #7
0
        public async void Patch_Record_Not_Found()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();
            var mockResult = new Mock <ActionResponse>();

            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiPostResponseModel>(null));
            PostController controller = new PostController(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 <ApiPostRequestModel>();

            patch.Replace(x => x.AcceptedAnswerId, 1);

            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>()));
        }
Пример #8
0
        public async void Create_No_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();

            var mockResponse = ValidationResponseFactory <ApiPostServerResponseModel> .CreateResponse(null as ApiPostServerResponseModel);

            mockResponse.SetRecord(new ApiPostServerResponseModel());
            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiPostServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiPostServerResponseModel> >(mockResponse));
            PostController controller = new PostController(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 ApiPostServerRequestModel());

            response.Should().BeOfType <CreatedResult>();
            (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created);
            var createResponse = (response as CreatedResult).Value as CreateResponse <ApiPostServerResponseModel>;

            createResponse.Record.Should().NotBeNull();
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiPostServerRequestModel>()));
        }
Пример #9
0
        public async void BulkInsert_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();

            var mockResponse = new Mock <CreateResponse <ApiPostServerResponseModel> >(null as ApiPostServerResponseModel);

            mockResponse.SetupGet(x => x.Success).Returns(false);

            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiPostServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiPostServerResponseModel> >(mockResponse.Object));
            PostController controller = new PostController(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 <ApiPostServerRequestModel>();

            records.Add(new ApiPostServerRequestModel());
            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 <ApiPostServerRequestModel>()));
        }