コード例 #1
0
        public async void CreateUser_ActionExecutes_ReturnResultWithDeviceDto()
        {
            DeviceDto newCustomer = new DeviceDto()
            {
                Id = 3, CustomerId = 1
            };

            _mockService.Setup(x => x.AddAsync(newCustomer)).ReturnsAsync(Response <DeviceDto> .Success(customers.Find(x => x.Id == 1), 200));
            var result = await _controller.Create(newCustomer);

            Assert.IsType <ObjectResult>(result);
        }
コード例 #2
0
        public void CreateTest()
        {
            PartialViewResult rv = (PartialViewResult)_controller.Create();

            Assert.IsInstanceOfType(rv.Model, typeof(DeviceVM));

            DeviceVM vm = rv.Model as DeviceVM;
            Device   v  = new Device();

            v.ID        = "44tvMxOP9";
            v.ProductId = AddProduct();
            vm.Entity   = v;
            _controller.Create(vm);

            using (var context = new DataContext(_seed, DBTypeEnum.Memory))
            {
                var data = context.Set <Device>().FirstOrDefault();

                Assert.AreEqual(data.ID, "44tvMxOP9");
            }
        }
コード例 #3
0
        public async Task Create_WithInvalidDevice_ReturnsModelError()
        {
            // Arrange
            var dbContext  = Setup.GetDbContext();
            var controller = new DeviceController(dbContext);

            controller.ModelState.AddModelError("Name", "Required");

            // Act
            var result = await controller.Create(new DeviceViewModel());

            // Assert
            var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestObjectResult.Value);
            Assert.Empty(dbContext.Devices);
        }
コード例 #4
0
        public void ShouldCreateANewWaterMeter()
        {
            var form = new DeviceForm
            {
                Type            = DeviceType.WATER_METER,
                ID              = "42",
                SerialNumber    = "42",
                FirmwareVersion = "1.0",
                State           = "Connected"
            };

            var result = (RedirectToRouteResult)controllerUT.Create(form);

            dbAccessMock.Verify(
                dbm => dbm.SaveDevice(It.Is <IDevice>(d => d is WaterMeter)),
                Times.Once());

            Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
        }
コード例 #5
0
        public async void Create_Errors()
        {
            DeviceControllerMockFacade mock = new DeviceControllerMockFacade();

            var mockResponse = new Mock <CreateResponse <ApiDeviceResponseModel> >(new FluentValidation.Results.ValidationResult());
            var mockRecord   = new ApiDeviceResponseModel();

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

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

            response.Should().BeOfType <ObjectResult>();
            (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity);
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiDeviceRequestModel>()));
        }
コード例 #6
0
        public async void Create_No_Errors()
        {
            DeviceControllerMockFacade mock = new DeviceControllerMockFacade();

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

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

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

            createResponse.Record.Should().NotBeNull();
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiDeviceRequestModel>()));
        }
コード例 #7
0
        public async Task Create_WithValidDevice_ReturnsCreatedDevice()
        {
            // Arrange
            var dbContext  = Setup.GetDbContext();
            var controller = new DeviceController(dbContext);

            // Act
            var result = await controller.Create(new DeviceViewModel()
            {
                Name = "TestDevice"
            });

            // Assert
            var okObjectResult = Assert.IsType <OkObjectResult>(result);
            var device         = Assert.IsAssignableFrom <DeviceViewModel>(okObjectResult.Value);

            Assert.Equal(1, device.Id);
            Assert.Equal("TestDevice", device.Name);
            var dbDevice = Assert.Single(dbContext.Devices);

            Assert.Equal("TESTDEVICE", dbDevice.NormalizedName);
        }