public async Task ValidateCart_RuleSetDefault_Valid() { // Arrange var aggregate = GetValidCartAggregate(); // Act var result = await _validator.ValidateAsync(aggregate, ruleSet : "default"); // Assert result.IsValid.Should().BeTrue(); result.Errors.Should().BeEmpty(); }
public async Task ValidateCart_EmptyCart_Valid() { // Arrange var aggregate = GetValidCartAggregate(); aggregate.Cart.Items.Clear(); aggregate.Cart.Shipments.Clear(); aggregate.Cart.Payments.Clear(); // Act var result = await _validator.ValidateAsync(new CartValidationContext { CartAggregate = aggregate }, options => options.IncludeRuleSets("default", "items", "shipments", "payments")); // Assert result.IsValid.Should().BeTrue(); result.Errors.Should().BeEmpty(); }
public async Task ValidateCart_RuleSetDefault_Valid() { //Arrange var cartService = new Moq.Mock <ICartService>(); //Act var validator = new CartValidator(cartService.Object); var cart = GetValidCart(); var result = await validator.ValidateAsync(cart, ruleSet : "default"); //Assertion Assert.True(result.IsValid); Assert.Empty(result.Errors); //Assert.Empty(cart.ValidationErrors); }
public async Task <IActionResult> Remove([FromBody] RemoveItemFromCartRequest request) { try { var validator = new CartValidator <RemoveItemFromCartRequest>(); var result = await validator.ValidateAsync(request); if (!result.IsValid) { throw new ValidationException("Invalid data"); } await _cartService.RemoveFromCart(request, User); return(Ok()); } catch (Exception) { return(BadRequest()); } }
public async Task ValidateCart_RuleSetDefault_Invalid() { //Arrange var cartService = new Moq.Mock <ICartService>(); //Act var validator = new CartValidator(cartService.Object); var cart = GetInvalidCart(); var result = await validator.ValidateAsync(cart, ruleSet : "default"); //Assertion Assert.False(result.IsValid); //Assert.NotEmpty(cart.ValidationErrors); Assert.NotEmpty(result.Errors); Assert.Equal(4, result.Errors.Count); Assert.Collection(result.Errors, x => { Assert.Equal(nameof(cart.Name), x.PropertyName); Assert.Equal(nameof(NotNullValidator), x.ErrorCode); } , x => { Assert.Equal(nameof(cart.Name), x.PropertyName); Assert.Equal(nameof(NotEmptyValidator), x.ErrorCode); } , x => { Assert.Equal(nameof(cart.CustomerId), x.PropertyName); Assert.Equal(nameof(NotNullValidator), x.ErrorCode); } , x => { Assert.Equal(nameof(cart.CustomerId), x.PropertyName); Assert.Equal(nameof(NotEmptyValidator), x.ErrorCode); } ); }