public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { List <ValidationResult> validationResult = new List <ValidationResult>(); if (string.IsNullOrWhiteSpace(this.Unit)) { validationResult.Add(new ValidationResult("Unit is required", new List <string> { "Unit" })); } if (validationResult.Count.Equals(0)) { /* Service Validation */ UomService service = (UomService)validationContext.GetService(typeof(UomService)); if (service.DbContext.Set <Uom>().Count(r => r._IsDeleted.Equals(false) && r.Id != this.Id && r.Unit.Equals(this.Unit)) > 0) /* Unit Unique */ { validationResult.Add(new ValidationResult("Unit already exists", new List <string> { "Unit" })); } } return(validationResult); }
public void GetSimple_Return_InternalServerError() { //Setup Mock <IServiceProvider> serviceProviderMock = GetServiceProvider(); UomService service = new UomService(serviceProviderMock.Object); serviceProviderMock.Setup(s => s.GetService(typeof(UomService))).Returns(service); //Act IActionResult response = GetController(service).GetSimple(); //Assert int statusCode = this.GetStatusCode(response); Assert.Equal((int)HttpStatusCode.InternalServerError, statusCode); }
public void GetSimple_Return_OK() { //Setup CoreDbContext dbContext = GetDbContext(GetCurrentAsyncMethod()); Mock <IServiceProvider> serviceProviderMock = GetServiceProvider(); UomService service = new UomService(serviceProviderMock.Object); serviceProviderMock.Setup(s => s.GetService(typeof(UomService))).Returns(service); serviceProviderMock.Setup(s => s.GetService(typeof(CoreDbContext))).Returns(dbContext); Lib.Models.Uom testData = GetTestData(dbContext); //Act IActionResult response = GetController(service).GetSimple(); //Assert int statusCode = this.GetStatusCode(response); Assert.Equal((int)HttpStatusCode.OK, statusCode); }
protected UomsController GetController(UomService service) { var user = new Mock <ClaimsPrincipal>(); var claims = new Claim[] { new Claim("username", "unittestusername") }; user.Setup(u => u.Claims).Returns(claims); UomsController controller = new UomsController(service); controller.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user.Object } }; controller.ControllerContext.HttpContext.Request.Headers["Authorization"] = "Bearer unittesttoken"; controller.ControllerContext.HttpContext.Request.Path = new PathString("/v1/unit-test"); return(controller); }
public UomController() { _uomService = new UomService(); }