Skip to content

Sample solution featuring entity framework core unit tests

Notifications You must be signed in to change notification settings

simaosoares/EntityFrameworkCoreTests

Repository files navigation

Entity Framework Core Tests

Sample solution featuring entity framework core unit tests.

Table of contents:

Create Solution

Type the following to initialize an empty solution:

mkdir EntityFrameworkCoreTests
cd EntityFrameworkCoreTests
dotnet new sln

Project List

The solution is composed by the following projects:

EntityFrameworkCoreTests.Data

This project contains all the required entities and the database context.

Type the following steps to add the EntityFrameworkCoreTests.Data project to the existing solution.

dotnet new classlib -n EntityFrameworkCoreTests.Data -f netcoreapp2.2
dotnet sln add EntityFrameworkCoreTests.Data

The option -f netcoreapp2.2 specifies the target framework for the project.

The following line will add the reference to Microsoft.EntityFrameworkCore package to the project necessary to build the database context.

dotnet add package Microsoft.EntityFrameworkCore

EntityFrameworkCoreTests.Services

This project contains the application services.

Type the following steps to add the EntityFrameworkCoreTests.Services project to the existing solution.

dotnet new classlib -n EntityFrameworkCoreTests.Services -f netcoreapp2.2
dotnet sln add EntityFrameworkCoreTests.Services

Since the project needs a reference to EntityFrameworkCoreTests.Data type the following command on the EntityFrameworkCoreTests.Services root folder to add the dependency.

dotnet add reference ../EntityFrameworkCoreTests.Data/EntityFrameworkCoreTests.Data.csproj

The file EntityFrameworkCoreTests.Services.csproj is updated with the required reference:

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <ProjectReference Include="..\EntityFrameworkCoreTests.Data\EntityFrameworkCoreTests.Data.csproj" />
  </ItemGroup>

</Project>

EntityFrameworkCoreTests.Tests

This project contains the application persistence and services unit tests for the libraries EntityFrameworkCoreTests.Data and EntityFrameworkCoreTests.Services using the XUnit unit testing tool combined with an in-memory database.

Type the following steps to add the EntityFrameworkCoreTests.Tests project to the existing solution.

dotnet new xunit -n EntityFrameworkCoreTests.Tests
dotnet sln add EntityFrameworkCoreTests.Tests

The following commands will add the required project dependencies:

dotnet add reference ../EntityFrameworkCoreTests.Data/EntityFrameworkCoreTests.Data.csproj
dotnet add reference ../EntityFrameworkCoreTests.Services/EntityFrameworkCoreTests.Services.csproj

The InMemory provider is useful to test components using something that approximates connecting to the real database, without the overhead of actual database operations.

EF Core database providers do not have to be relational databases. InMemory is designed to be a general purpose database for testing, and is not designed to mimic a relational database.

  • InMemory will allow you to save data that would violate referential integrity constraints in a relational database.
  • If you use DefaultValueSql(string) for a property in your model, this is a relational database API and will have no effect when running against InMemory.

The following command will add the required package to run our tests with an in-memory database.

dotnet add package Microsoft.EntityFrameworkCore.InMemory -v 2.2.6

The following command will add the required package to run our tests with a relational database Sqlite that will be configured to run as in-memory data source.

dotnet add package Microsoft.EntityFrameworkCore.Sqlite
// ...
new SqliteConnection("DataSource=:memory:");
// ...

Build Solution

dotnet clean
dotnet build

Test Solution

Type the following command to run the solution unit tests:

dotnet test

References

About

Sample solution featuring entity framework core unit tests

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages