Created
July 14, 2020 11:24
-
-
Save B3nCr/155ea20a981f1b2d391fa848340e27f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using FluentValidation; | |
using Xunit; | |
namespace Tests | |
{ | |
public class UnitTest1 | |
{ | |
[Theory] | |
[InlineData("ben",11)] | |
[InlineData("be",11)] | |
[InlineData("ben",9)] | |
[InlineData("be",9)] | |
public void TestValidation(string name, int age) | |
{ | |
var parent = new Parent(name, new Child(age)); | |
var validator = new ParentValidator(); | |
validator.ValidateAndThrow(parent); | |
} | |
[Theory] | |
[InlineData("VALID")] | |
[InlineData("No")] | |
public void TestNullChild(string name) | |
{ | |
var parent = new Parent(name, null); | |
var validator = new ParentValidator(); | |
validator.ValidateAndThrow(parent); | |
} | |
} | |
public class Parent | |
{ | |
public Parent(string name, Child child) | |
{ | |
Name = name; | |
Child = child; | |
} | |
public string Name { get; set; } | |
public Child Child { get; set; } | |
} | |
public class Child | |
{ | |
public Child(int age) | |
{ | |
Age = age; | |
} | |
public int Age { get; set; } | |
} | |
public class ChildValidator : AbstractValidator<Child> | |
{ | |
public ChildValidator() | |
{ | |
RuleFor(x => x.Age) | |
.GreaterThan(10) | |
.WithMessage("Age is too young"); | |
} | |
} | |
public class ParentValidator : AbstractValidator<Parent> | |
{ | |
public ParentValidator() | |
{ | |
RuleFor(x => x.Name) | |
.MinimumLength(3); | |
RuleFor(x=>x.Child) | |
.NotNull() | |
.WithErrorCode("not null code") | |
.WithMessage("not null message") | |
.SetValidator(new ChildValidator()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment