public void WhenConstructed_ThenSuccessful() { var actual = new VehicleFormModel(); Assert.NotNull(actual); Assert.Equal(0, actual.VehicleId); Assert.Null(actual.Name); }
public void WhenIdSet_ThenValueUpdated() { var target = new VehicleFormModel(); target.VehicleId = 4; int actual = target.VehicleId; Assert.Equal(4, actual); }
public void WhenNameSet_ThenValueUpdated() { var target = new VehicleFormModel(); target.Name = "Name"; string actual = target.Name; Assert.Equal("Name", actual); }
public void WhenNameSetToNull_ThenUpdatesValue() { var target = new VehicleFormModel(); target.Name = "Name"; target.Name = null; string actual = target.Name; Assert.Null(actual); }
public void InvokesVehicleRepository() { var vehicleForm = new VehicleFormModel { Name = "vehicle" }; var handler = new CreateVehicle(_vehicleRepo.Object, _photoRepo.Object); handler.Execute(UserId, vehicleForm, null); _vehicleRepo .Verify(r => r.Create(UserId, It.IsAny<Vehicle>()), Times.Once()); }
public void WhenNameSetToValidValue_ThenValidationPasses() { var target = new VehicleFormModel(); target.Name = "Name"; var validationContext = new ValidationContext(target, null, null); var validationResults = new List<ValidationResult>(); bool actual = Validator.TryValidateObject(target, validationContext, validationResults, true); Assert.True(actual); Assert.Equal(0, validationResults.Count); }
public void AndVehicleRepositoryThrows_ThenWrapsException() { _vehicleRepo .Setup(v => v.Create(It.IsAny<int>(), It.IsAny<Vehicle>())) .Throws<InvalidOperationException>(); var vehicleForm = new VehicleFormModel { Name = "vehicle" }; var handler = new CreateVehicle(_vehicleRepo.Object, _photoRepo.Object); var ex = Assert.Throws<BusinessServicesException>(() => handler.Execute(UserId, vehicleForm, null)); Assert.IsType<InvalidOperationException>(ex.InnerException); }
public void ThenDelegatesToVehicleRepository() { var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId)) .Returns(new Model.Vehicle()); var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); handler.Execute(UserId, vehicleForm, null); _vehicleRepo.Verify(r => r.Update(It.IsAny<Model.Vehicle>()), Times.Once()); }
public void WithAPhoto_ThenInvokesVehicleRepositoryToUpdatePhotoInfo() { var vehicleForm = new VehicleFormModel { Name = "vehicle" }; var photoStream = Mock.MockPhotoStream(); var handler = new CreateVehicle(_vehicleRepo.Object, _photoRepo.Object); handler.Execute(UserId, vehicleForm, photoStream.Object); _vehicleRepo .Verify(r => r.Create(UserId, It.IsAny<Vehicle>()), Times.Once()); _vehicleRepo .Verify(r => r.Update(It.IsAny<Vehicle>()), Times.Once()); }
public void ForOtherUser_ThenThrows() { const int anotherUserId = 87; var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = DefaultVehicleId }; _vehicleRepo .Setup(vr => vr.GetVehicle(anotherUserId, DefaultVehicleId)) .Throws(new InvalidOperationException()); var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); Assert.Throws<BusinessServicesException>(() => handler.Execute(anotherUserId, vehicleForm, null)); }
public void WhenMakeNameSetTo51Characters_ThenValidationFails() { var target = new VehicleFormModel(); target.Name = "Name"; target.MakeName = new string('1', 51); var validationContext = new ValidationContext(target, null, null); var validationResults = new List<ValidationResult>(); bool actual = Validator.TryValidateObject(target, validationContext, validationResults, true); Assert.False(actual); Assert.Equal(1, validationResults.Count); Assert.Equal(1, validationResults[0].MemberNames.Count()); Assert.Equal("MakeName", validationResults[0].MemberNames.First()); }
public void WihtNewPhoto_ThenDelegatesToPhotoRepositoryAddNewPhoto() { var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId }; _vehicleRepo .Setup(r => r.GetVehicle(UserId, DefaultVehicleId)) .Returns(new Model.Vehicle { VehicleId = DefaultVehicleId }); var newPhotoFile = Mock.MockPhotoStream().Object; var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); handler.Execute(UserId, vehicleForm, newPhotoFile); _photoRepo.Verify(r => r.Create(DefaultVehicleId, It.IsAny<Model.VehiclePhoto>()), Times.Once()); }
public void ThatAndVehicleDoesNotExist_ThenThrowsNonExistent_ThenThrows() { // this is the same test as FromOtherUsers_ThenThrows const int nonExistentVehicleId = 87; var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = nonExistentVehicleId }; // the repo throws an exception when it can't find a match with both the user and the vehicle _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, It.IsAny<int>())) .Throws(new InvalidOperationException()); var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); Assert.Throws<BusinessServicesException>(() => handler.Execute(UserId, vehicleForm, null)); }
public void WithExistingPhoto_ThenDelegatesToPhotoRepositoryToDeleteOldPhoto() { const int vehiclePhotoId = 300; var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId)) .Returns(new Model.Vehicle { VehicleId = DefaultVehicleId, PhotoId = vehiclePhotoId }); var newPhotoFile = Mock.MockPhotoStream().Object; var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); handler.Execute(UserId, vehicleForm, newPhotoFile); _photoRepo.Verify(r => r.Delete(vehiclePhotoId), Times.Once()); }
public void ThenNoValidationErrorsAreReturned() { var vehicleForm = new VehicleFormModel { Name = "vehicle" }; var vehicles = new List<Vehicle> { new Vehicle() }; _vehicleRepo .Setup(vr => vr.GetVehicles(UserId)) .Returns(vehicles); var subhandler = MockCanValidateVehicleYearMakeAndModel(vehicleForm); var handler = new CanAddVehicle(_vehicleRepo.Object, subhandler); var actual = handler.Execute(UserId, vehicleForm); Assert.Empty(actual); }
public void WithTooManyVehicles_ThenReturnsValidationResult() { var vehicleForm = new VehicleFormModel { Name = "vehicle" }; var vehicles = Enumerable .Range(0, CanAddVehicle.MaxNumberOfVehiclesPerUser) .Select(i => new Vehicle()); _vehicleRepo .Setup(vr => vr.GetVehicles(UserId)) .Returns(vehicles); var subhandler = MockCanValidateVehicleYearMakeAndModel(vehicleForm); var handler = new CanAddVehicle(_vehicleRepo.Object, subhandler); var actual = handler.Execute(UserId, vehicleForm).ToList(); Assert.Equal(1, actual.Count); Assert.Contains("maximum number", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); }
public void Throws_ThenWrapsException() { var vehicleForm = new VehicleFormModel { Name = "vehicle", VehicleId = DefaultVehicleId }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId)) .Throws(new InvalidOperationException()); var handler = new UpdateVehicle(_vehicleRepo.Object, _photoRepo.Object); Exception ex = Assert.Throws<BusinessServicesException>(() => handler.Execute(UserId, vehicleForm, null)); Assert.NotNull(ex.InnerException); Assert.IsType<InvalidOperationException>(ex.InnerException); }
public void WithInvalidMake_ThenReturnsValidationResult() { var vehicleForm = new VehicleFormModel { Name = "vehicle", Year = 1, MakeName = "Test", ModelName = "Test" }; SetupManufacturerRepo(isMakeValid: false); var handler = new CanValidateVehicleYearMakeAndModel(_manufacturerRepo.Object); var actual = handler.Execute(vehicleForm).ToList(); Assert.Equal(1, actual.Count); Assert.Contains("not valid", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); Assert.Contains("make", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); }
private static CanValidateVehicleYearMakeAndModel MockCanValidateVehicleYearMakeAndModel(VehicleFormModel vehicleForm) { var subhandler = new Mock<CanValidateVehicleYearMakeAndModel>(null); subhandler .Setup(h => h.Execute(vehicleForm)) .Returns(Enumerable.Empty<ValidationResult>()); return subhandler.Object; }
public void WithMissingYearForModel_ThenReturnsValidationResult() { var vehicleForm = new VehicleFormModel { Name = "vehicle", ModelName = "Test" }; _manufacturerRepo .Setup(x => x.IsValidYear(It.IsAny<int>())) .Returns(true); _manufacturerRepo .Setup(x => x.IsValidMake(It.IsAny<int>(), It.IsAny<string>())) .Returns(true); _manufacturerRepo .Setup(x => x.IsValidModel(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(true); var handler = new CanValidateVehicleYearMakeAndModel(_manufacturerRepo.Object); var actual = handler.Execute(vehicleForm).ToList(); Assert.Equal(1, actual.Count); Assert.Contains("missing", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); Assert.Contains("year", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); Assert.Contains("model", actual[0].Message, StringComparison.CurrentCultureIgnoreCase); }
public void WhenSortOrderSet_ThenValueUpdated() { var target = new VehicleFormModel(); target.SortOrder = 1; int actual = target.SortOrder; Assert.Equal(1, actual); }
public void WhenSortOrderSetToNegative_ThenValidationFails() { var target = new VehicleFormModel(); target.Name = "Name"; target.SortOrder = -3; var validationContext = new ValidationContext(target, null, null); var validationResults = new List<ValidationResult>(); bool actual = Validator.TryValidateObject(target, validationContext, validationResults, true); Assert.False(actual); Assert.Equal(1, validationResults.Count); Assert.Equal(1, validationResults[0].MemberNames.Count()); Assert.Equal("SortOrder", validationResults[0].MemberNames.First()); }
public void WhenYearSet_ThenValueUpdated() { var target = new VehicleFormModel(); target.Year = 1971; int? actual = target.Year; Assert.True(actual.HasValue); Assert.Equal(1971, actual.Value); }
public void WhenYearSetToNull_ThenUpdatesValue() { var target = new VehicleFormModel(); target.Year = 1971; target.Year = null; int? actual = target.Year; Assert.False(actual.HasValue); }