public void AddFarmCrop_InvalidAgricultureId_ThrowValidationException() { /* * CR-1 - structure test in Arrange-Act-Assert manner */ FarmCropDto enemy = new FarmCropDto() { AgricultureId = -1, FarmerId = 1, RegionId = 1, Area = 1, Gather = 1, Name = "abc" }; try { farmService.AddFarmCrop(enemy); } /* * CR-1 - * Use Asset.Throws<> instead of try-catch block, because if excation is not be throwed, the test will pass * Target code here: * var ex = Assert.Throws<ValidationException>(() => farmService.AddFarmCrop(enemy)); * Assert.AreEqual(ex.Property, "AgricultureId"); * * Use Assert.AreEqual istead of Assert.IsTrue with lambda, because of more meaningfull output */ catch (ValidationException ex) { Assert.IsTrue(ex.Property == "AgricultureId"); } }
public void AddFarmCrop_ValidateModelMappingWhenSave_IsValid_() { /* * CR-1 - structure test in Arrange-Act-Assert manner */ bool isValid = false; var cropRepo = new Mock <IRepository <Crop> >(); cropRepo.Setup(item => item.Create(It.IsAny <Crop>())).Callback <Crop>(arg => { isValid = arg.AgricultureId == 1 && arg.Gather == 5 && arg.CropFarm.Name == "abc" && arg.CropFarm.FarmerId == 2 && arg.CropFarm.RegionId == 3; }); var iow = new Mock <IUnitOfWork>(); iow.Setup(item => item.Crops).Returns(cropRepo.Object); farmService = new FarmService(iow.Object, AutoMapperConfig.GetMapper()); FarmCropDto enemy = new FarmCropDto() { AgricultureId = 1, FarmerId = 2, RegionId = 3, Area = 4, Gather = 5, Name = "abc" }; farmService.AddFarmCrop(enemy); Assert.IsTrue(isValid); }
public void AddFarmCrop_InvalidAgricultureId_ThrowValidationException() { FarmCropDto enemy = new FarmCropDto() { AgricultureId = -1, FarmerId = 1, RegionId = 1, Area = 1, Gather = 1, Name = "abc" }; try { farmService.AddFarmCrop(enemy); } catch (ValidationException ex) { Assert.IsTrue(ex.Property == "AgricultureId"); } }
public void AddFarmCrop_InvalidArea_ThrowValidationException() { var cropRepo = new Mock <IRepository <Crop> >(); cropRepo.Setup(item => item.Create(It.IsAny <Crop>())).Callback(() => { }); var iow = new Mock <IUnitOfWork>(); iow.Setup(item => item.Crops).Returns(cropRepo.Object); var farmService = new FarmService(iow.Object, AutoMapperConfig.GetMapper()); FarmCropDto enemy = new FarmCropDto() { AgricultureId = 1, FarmerId = 1, RegionId = 1, Area = 0, Gather = 1, Name = "abc" }; try { farmService.AddFarmCrop(enemy); } catch (ValidationException ex) { Assert.IsTrue(ex.Property == "Area"); } }