public async Task Validator_ShouldFail_CodeEmpty() { var entity = new Classification() { Id = Guid.NewGuid(), Code = Guid.NewGuid().ToString(), }; // setup data using (var db = _dbHelper.GetDbContext()) { db.Classifications.RemoveRange(db.Classifications); await db.Classifications.AddAsync(entity); await db.SaveChangesAsync(); } // run operation using (var db = _dbHelper.GetDbContext()) { var validator = new UpdateClassificationCommandValidator(db);; var command = new UpdateClassificationCommand() { Id = entity.Id, Code = "" }; var result = await validator.ValidateAsync(command); // check result result.IsValid.ShouldBeFalse(); result.Errors.Count.ShouldEqual(1); result.Errors.First().PropertyName.ShouldEqual(nameof(command.Code)); } }
public async Task Validator_ShouldPass_GoodData() { var entity = new Classification() { Id = Guid.NewGuid(), Code = Guid.NewGuid().ToString(), }; // setup data using (var db = _dbHelper.GetDbContext()) { db.Classifications.RemoveRange(db.Classifications); await db.Classifications.AddAsync(entity); await db.SaveChangesAsync(); } // run operation using (var db = _dbHelper.GetDbContext()) { var validator = new UpdateClassificationCommandValidator(db); var command = new UpdateClassificationCommand() { Id = entity.Id, Code = entity.Code + "Updated" }; // check result var result = await validator.ValidateAsync(command); result.IsValid.ShouldBeTrue(); } }
public async Task <IActionResult> Update(Guid id, [FromBody] UpdateClassificationCommand command) { if (command.Id == id) { return(await _commandSender.ValidateAndSendAsync(command, ModelState)); } ModelState.AddModelError("Id", ValidationMessages.RouteParameterMustMatchFormDataParameter); return(BadRequest(ModelState)); }
public async Task Validator_ShouldFail_NoIdFound() { // run operation using (var db = _dbHelper.GetDbContext()) { var validator = new UpdateClassificationCommandValidator(db);; var command = new UpdateClassificationCommand() { Id = Guid.NewGuid(), Code = Guid.NewGuid() + "" }; var result = await validator.ValidateAsync(command); // check result result.IsValid.ShouldBeFalse(); result.Errors.Count.ShouldEqual(1); result.Errors.First().PropertyName.ShouldEqual(nameof(command.Id)); } }
public async Task Command_ShouldPass_GoodData() { var entity = new Classification() { Id = Guid.NewGuid(), Code = Guid.NewGuid().ToString(), }; // setup data using (var db = _dbHelper.GetDbContext()) { db.Classifications.RemoveRange(db.Classifications); await db.Classifications.AddAsync(entity); await db.SaveChangesAsync(); } // run operation using (var db = _dbHelper.GetDbContext()) { var handler = new UpdateClassificationCommandHandler(db); var command = new UpdateClassificationCommand() { Id = entity.Id, Code = entity.Code + "Updated" }; await handler.ExecuteAsync(command); } // check result using (var db = _dbHelper.GetDbContext()) { db.Classifications.ShouldNotBeEmpty(); (await db.Classifications.CountAsync()).ShouldEqual(1); (await db.Classifications.SingleAsync()).Code.ShouldEndWith("Updated"); } }