DEV Community

Cover image for From TDD to DDD: Building a .NET Core Web API - Part 2

From TDD to DDD: Building a .NET Core Web API - Part 2

lucianopereira86 profile image LUCIANO DE SOUSA PEREIRA ・2 min read

The complete project can be found here:



Test Project

It's time to begin the fun!

We will build tests for each CRUD method of the User entity by using Entity Framework Core structure.
There will be two types of unit tests: Fact and Theory.

Fact is a method with a unique result without parameters.

Theory allow multiple parameters expecting for different results.

Post User

Add a folder named "Tests" to the Test Project with a file named "PostUserTest.cs" with the code below:

using Xunit;

namespace CRUD_NETCore_TDD.Test.Tests
    public class PostUserTest
        #region THEORY
        #region FACT
        public void Fact_PostUser ()



Red Step

Our first test will run what we really want: to register a new user to the database. Write this code inside the Fact_PostUser method:

public void Fact_PostUser ()
    // EXAMPLE
    var user = new User("LUCIANO PEREIRA", 33, true);


    // ASSERT
    Assert.Equal(1, user.Id);

At first, there are no User class. Also, the "ctx" object should be an instance of the DbContext class, but there is no EF Core library installed yet to make the code to compile.
That is the Red step. We know what we want and what we have to do.

Before going to the Green step, change the method's name from "Fact_PostUser" to "Fact_PostUser_NoModelNoRepository", so it becomes clear what is missing for the method to run.


Let's implement the Green step.

Discussion (0)

Editor guide