public void Should_not_create_or_update_vehicle_on_post_put_when_provided_invalid_make() { //Arrange var _vehicleRepo = new Mock <IRepository <VehicleModel> >(); var id = Guid.NewGuid(); var post = new PostVehicleModel() { Make = null, Model = "test model", Odometer = 0, Plate = "test plate", VehicleType = VehicleTypeEnum.Electric, Year = 0, }; _controller = new VehiclesController(_vehicleRepo.Object, _serviceRepo.Object); //Act var result = JsonConvert.DeserializeObject <ResultModel>(_controller.Post(post)); var result1 = JsonConvert.DeserializeObject <ResultModel>(_controller.Put(post.ToVehicleModel())); //Assert Assert.True(result.IsError); Assert.True(result1.IsError); }
public async void Post_WhenConflict() { // arrange var testVehicle = TestVehicleModel(); var errorCodeConverter = new ErrorCodeConverter(); var vehicleServiceMoq = new Mock <IVehicleService>(); vehicleServiceMoq.Setup(x => x.Insert(It.IsAny <Vehicle>())) .ReturnsAsync(() => new Result <Guid>(ResultCode.Conflict, testVehicle.Id)); var dataStructureConverterMoq = new Mock <IDataStructureConverter>(); var sut = new VehiclesController(vehicleServiceMoq.Object, errorCodeConverter, dataStructureConverterMoq.Object) { ControllerContext = DefaultControllerContext() }; // act var result = await sut.Post(testVehicle); var conflictResult = result as StatusCodeResult; // assert Assert.Equal(409, conflictResult.StatusCode); }
public void Should_create_or_update_vehicle_on_post_put_when_provided_valid_post_vehicle() { //Arrange var _vehicleRepo = new Mock <IRepository <VehicleModel> >(); _vehicleRepo.Setup(moq => moq.Read()).Returns(new System.Collections.Generic.List <VehicleModel>() { new VehicleModel() { Plate = "test" } }); var id = Guid.NewGuid(); var post = new PostVehicleModel() { Make = "test make", Model = "test model", Odometer = 0, Plate = "test plate", VehicleType = VehicleTypeEnum.Electric, Year = 0, }; _controller = new VehiclesController(_vehicleRepo.Object, _serviceRepo.Object); //Act var result = JsonConvert.DeserializeObject <ResultModel>(_controller.Post(post)); var result1 = JsonConvert.DeserializeObject <ResultModel>(_controller.Put(post.ToVehicleModel())); //Assert Assert.False(result.IsError); Assert.False(result1.IsError); }
public async Task Post_WhenCreateAVehicle_ShouldReturnsTheCreatedVehicle() { //Arrenge var newVehicleDescription = "VW Golf"; var newVehiclePrice = 10; _serviceMock.Setup(s => s.AddNewVehicle(It.IsAny <VehicleDto>())) .ReturnsAsync(new VehicleDto { Id = 1, Description = newVehicleDescription, PricePerDay = newVehiclePrice }); var newVehicleRequest = new VehicleRequestModel { Description = newVehicleDescription, PricePerDay = newVehiclePrice }; //Act var result = await _sut.Post(newVehicleRequest); //Assert result.Should().BeOfType(typeof(CreatedAtActionResult)); var value = result.As <ObjectResult>().Value.As <VehicleResponseModel>(); value.Id.Should().Be(1); value.Description.Should().Be(newVehicleDescription); value.PricePerDay.Should().Be(newVehiclePrice); _serviceMock.Verify(s => s.AddNewVehicle(It.Is <VehicleDto>(c => c.Description == newVehicleDescription && c.PricePerDay == newVehiclePrice)), Times.Once); }
public void PostWithMissingVehicleResultsInBadRequest() { var mock = new Mock <IVehicleRepositorical>(); var controller = new VehiclesController(mock.Object); var result = controller.Post(null); var badRequestResult = Assert.IsType <BadRequestObjectResult>(result); }
public async void AddVehicle_ReturnVehicleCreated() { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.SaveAll()) .Returns(Task.FromResult <bool>(true)); var mapper = _dependencyFixture.ServiceProvider.GetService <IMapper>(); var logger = Mock.Of <ILogger <VehiclesController> >(); var controller = new VehiclesController(mockRepo.Object, mapper, logger); VehicleForNew newVehicle = new VehicleForNew() { value = 18000.50M, brandId = 1, modelId = 1, yearModel = 1998, fuel = "Gasoline" }; // Act var result = await controller.Post(newVehicle); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Vehicle created.", returnValue); }
public async void Post_WhenNotValid() { // arrange var errorCodeConverter = new ErrorCodeConverter(); var vehicleServiceMoq = new Mock <IVehicleService>(); vehicleServiceMoq.Setup(x => x.Insert(It.IsAny <Vehicle>())) .ReturnsAsync(() => new Result <Guid>(ResultCode.BadRequest, TestVehicle().Id)); var dataStructureConverterMoq = new Mock <IDataStructureConverter>(); var sut = new VehiclesController(vehicleServiceMoq.Object, errorCodeConverter, dataStructureConverterMoq.Object) { ControllerContext = DefaultControllerContext() }; // act var result = await sut.Post(TestVehicleModel()); var badRequestResult = result as BadRequestResult; // assert Assert.NotNull(badRequestResult); }
public void Post_NullObject_Returns400() { controller = new VehiclesController(mockVehicleRepo.Object); var result = controller.Post(null); Assert.IsType(typeof(BadRequestResult), result); }
public void Post_CreateSuccessful_Returns201() { mockVehicleRepo.Setup(x => x.Create(It.IsAny <Vehicle>())).Returns(true); controller = new VehiclesController(mockVehicleRepo.Object); var result = controller.Post(GenerateTestVehicle(1)); Assert.IsType(typeof(CreatedAtRouteResult), result); }
public async Task POST_Create_RETURNS_OK() { _mockDataManager.Setup(manager => manager.CreateAsync(It.IsAny <Vehicle>())) .ReturnsAsync(It.IsAny <long>()); var vehicle = await _controller.Post(FakeCreateRequestObject()); var response = Assert.IsType <ApiResponse>(vehicle); Assert.Equal(201, response.StatusCode); }
public void Post_CreateUnsuccessful_Returns409() { mockVehicleRepo.Setup(x => x.Create(It.IsAny <Vehicle>())).Returns(false); controller = new VehiclesController(mockVehicleRepo.Object); var result = controller.Post(GenerateTestVehicle(1)); var statusObject = result as HttpStatusCodeResult; // No elegant way to produce or check a Conflict status code, so do this instead Assert.NotNull(statusObject); Assert.Equal(409, statusObject.StatusCode); }
public void Post() { // Arrange VehiclesController controller = new VehiclesController(); var vehicle = new Vehicle(1, "bats", "batmobile", 2016); // Act controller.Post(vehicle); var result = controller.Get(vehicle.Id); // Assert Assert.AreEqual(vehicle.Year, result.Year); }
//[Fact] public void RegisterVehicle() { //Arrange Vehicle vehicle = VehicleDataRequest.CreateCarVehicle(); //Act var result = valuesController.Post(vehicle); string json = JsonConvert.SerializeObject(result); Response response = JsonConvert.DeserializeObject <Response>(json); //Assert Assert.Equal(200, response.StatusCode); }
public void PostVehicleConstraintViolationResultsInBadRequest() { var mock = new Mock <IVehicleRepositorical>(); mock.Setup(m => m.FindById(It.Is <string>(s => s == "2"))).Returns(default(VehicleInfo)); mock.Setup(m => m.AddNewVehicle(It.IsAny <VehicleInfo>())).Throws <VehicleConstraintViolatedException>(); var controller = new VehiclesController(mock.Object); var result = controller.Post(new VehicleInfo { VehicleIdentifier = "name 3", VehicleIdentifier = "10.2" }); var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result); var errorResponse = Assert.IsType <ErrorResponseInfo>(badRequestObjectResult.Value); // TODO: Error object }
public void Should_not_create_or_update_vehicle_on_post_put_when_provided_null_post_vehicle() { //Arrange var _vehicleRepo = new Mock <IRepository <VehicleModel> >(); var _vehicleValidation = new Mock <IServicesValidation>(); _controller = new VehiclesController(_vehicleRepo.Object, _serviceRepo.Object); //Act var result = JsonConvert.DeserializeObject <ResultModel>(_controller.Post(null)); var result1 = JsonConvert.DeserializeObject <ResultModel>(_controller.Put(null)); //Assert Assert.True(result.IsError); Assert.True(result1.IsError); }
public async Task ShouldCreate() { var service = new Mock <IVehicleService>(); var controller = new VehiclesController(service.Object); var viewModel = new VehicleViewModel { Name = "Test Vehicle", EngineId = 1, WheelId = 1 }; service.Setup(x => x.CreateNewVehicle(viewModel)).Returns(Task.FromResult(true)); var result = await controller.Post(viewModel); Assert.IsType <NoContentResult>(result); }
public void PostVehicleResultsInCreate() { var mock = new Mock <IVehicleRepositorical>(); Vehicle newVehicle = null; mock.Setup(m => m.AddNewVehicle(It.IsAny <Vehicle>())) .Callback((Vehicle p) => { newVehicle = p; }) .Returns("3"); var controller = new VehiclesController(mock.Object); var result = controller.Post(new VehicleInfo { ModelIdentifier = "name 3", VehicleIdentifier = "10.2" }); var createdAtRouteResult = Assert.IsType <CreatedAtRouteResult>(result); var vehicle = Assert.IsType <VehicleInfo>(createdAtRouteResult.Value); Assert.Equal("name 3", vehicle.ModelIdentifier); Assert.Equal("3", vehicle.VehicleIdentifier); Assert.NotNull(newVehicle); }
public void AddVehicle_ShouldAlwaysAddVehicle() { var vehicleModel = new VehicleModel { Id = 1 }; var vehicles = new List <Vehicle>().AsQueryable(); var vehiclesMockSet = MockDbSetHelper.CreateMockSet(vehicles); var vehicleToAdd = new AddVehicleDto { Id = 1, VehicleModelId = 1, RegistrationNumber = "" }; var mockContext = new Mock <InstantDeliveryContext>(); mockContext.Setup(c => c.Vehicles).Returns(vehiclesMockSet.Object); mockContext.Setup(c => c.VehicleModels).ReturnsDbSet(vehicleModel); var controller = new VehiclesController(mockContext.Object); controller.Post(vehicleToAdd); vehiclesMockSet.Verify(m => m.Add(It.Is((Vehicle v) => v.Id == vehicleToAdd.Id)), Times.Once()); mockContext.Verify(m => m.SaveChanges(), Times.Once()); }