From TDD to DDD: Building a .NET Core Web API - Part 6
Tags: tdd, ddd, netcore, xunit
Published: 2019-12-16 12:23:20

The complete project can be found here: [https://github.com/lucianopereira86/CRUD-NetCore-TDD](https://github.com/lucianopereira86/CRUD-NetCore-TDD)

## Technologies

- [Visual Studio 2019](https://visualstudio.microsoft.com/pt-br/downloads/)
- [.NET Core 3.1.0](https://dotnet.microsoft.com/download/dotnet-core/3.1)
- [xUnit 2.4.0](https://www.nuget.org/packages/xunit/2.4.0)
- [Microsoft.EntityFrameworkCore 3.1.0](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/3.1.0)
- [FluentValidation 8.6.0](https://www.nuget.org/packages/fluentvalidation)

## Post User • Theory

### Refactor Step • Name

Change the "PostUserValidator" constructor by adding these lines:

```cs
public PostUserValidator()
{
    RuleFor(x => x.Name)
        .Cascade(CascadeMode.StopOnFirstFailure)
        .NotEmpty()
        .WithErrorCode("100")
        .MaximumLength(20)
        .WithErrorCode("101");
}
```

For the null or empty condition, it will return the custom error code 100.  
For the maximum length condition, it will return the custom error code 101.

Inside the "PostUserTest" class, copy the "Theory_PostUser_Name_Validation", comment the original, rename the copy to "Theory_PostUser_Name" and refactor the code:

```cs
[Theory]
[InlineData(null, 100)]
[InlineData("", 100)]
[InlineData("LUCIANO PEREIRA", 100)]
[InlineData("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 101)]
[InlineData("LUCIANO PEREIRA", 101)]
public void Theory_PostUser_Name(string Name, int ErrorCode)
{
    var user = new User
    {
        Name = Name
    };

    var val = new PostUserValidator().Validate(user);
    Assert.False(val.IsValid);

    if(!val.IsValid)
    {
        bool hasError = val.Errors.Any(a => a.ErrorCode.Equals(ErrorCode.ToString()));
        Assert.True(hasError);
    }
}
```

For each possible value there is an error code. The successful result will happen when the expected error code exists in the error list.  
Notice that the value "LUCIANO PEREIRA" appears twice because it will be tested for each validation.
Run the test and see the result:

![print11](https://thepracticaldev.s3.amazonaws.com/i/scc1jb0pjcifro8jof76.JPG)

As we have expected, only the valid value didn't return the error code it was expecting. It means that our validation is working perfectly!

Let's improve our refactoring a little more by adding a generic method inside the "BaseTest" class that will be useful for all the other CRUD Test classes that will be created:

```cs
// Add inside BaseTest.cs
protected void CheckError<T>(AbstractValidator<T> validator, int ErrorCode, T vm)
{
    var val = validator.Validate(vm);
    Assert.False(val.IsValid);

    if (!val.IsValid)
    {
        bool hasError = val.Errors.Any(a => a.ErrorCode.Equals(ErrorCode.ToString()));
        Assert.True(hasError);
    }
}
```

The "Theory_PostUser_Name" inside the "PostUserTest" class must be changed as well:

```cs
[Theory]
[InlineData(null, 100)]
[InlineData("", 100)]
[InlineData("LUCIANO PEREIRA", 100)]
[InlineData("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 101)]
[InlineData("LUCIANO PEREIRA", 101)]
public void Theory_PostUser_Name(string Name, int ErrorCode)
{
    var user = new User
    {
        Name = Name
    };
    CheckError(new PostUserValidator(), ErrorCode, user);
}
```

## Next 

It's time create a **Theory** for the "Age" attribute.