DbIntegrationTestHelpers 1.0.8
See the version list below for details.
dotnet add package DbIntegrationTestHelpers --version 1.0.8
NuGet\Install-Package DbIntegrationTestHelpers -Version 1.0.8
<PackageReference Include="DbIntegrationTestHelpers" Version="1.0.8" />
paket add DbIntegrationTestHelpers --version 1.0.8
#r "nuget: DbIntegrationTestHelpers, 1.0.8"
// Install DbIntegrationTestHelpers as a Cake Addin #addin nuget:?package=DbIntegrationTestHelpers&version=1.0.8 // Install DbIntegrationTestHelpers as a Cake Tool #tool nuget:?package=DbIntegrationTestHelpers&version=1.0.8
Introduction
A tiny helper class that contains some boilerplate code for creating DB integration tests in EntityFramework.
Remember - unit tests do not allow you to test everything that you should test.
Integration tests are good, especially if writing them is simple.
Don't take my word for it, have a read, e.g. here: https://blog.kentcdodds.com/write-tests-not-too-many-mostly-integration-5e8c7fff591c or here https://gist.github.com/alistairmgreen/ca3f7baddb737fd91e6bf7399ba6deeb
Why unit tests are not enough?
Installation
Available as a Nuget package - https://www.nuget.org/packages/DbIntegrationTestHelpers/
Just run Install-Package DbIntegrationTestHelpers
Usage
General
Regardless of which approach (instance or static) you choose as the base class for your test class, you gain access to a this.Context
property, which will contain your DbContext.
Your DbContext is required to have a constructor which allows passing connection string.
public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString){}
The base class allows also specifying a SeedAction, as follows:
protected override Action SeedAction => () =>
{
SomeStaticClass.DoSomeSeeding(this.Context);
this.DoSomeMoreSeeding();
//or even seed directly
this.Context.Products.Add(new Product(){Name="TheProduct"});
this.Context.SaveChanges();
};
Instance approach
Add IntegrationTestHelpersBase<T>
as base class for your test class. The type parameter should be your DbContext type.
You now have access to this.Context
which is instance of your DbContext - the database behind it will be dropped and recreated with each test method run - this means there is a little overhead for each test, during which a database is created and seeded. In my tests, the overhead is approximately 2 seconds for each test.
Static approach (faster, but tests are interdependent)
Add StaticContextIntegrationsTestsBase<T>
as base class for your test class. The type parameter should be your DbContext type.
You now have access to this.Context
which is a static object of your DbContext.
Due to that, bear in mind that the tests methods you write will have to be structured in a certain way (you cannot have each test reference a mock entity with ID 1).
The tests will use a new, test only database (LocalDb). The database is wiped only when a new test run is exectuted, so you can have a look at what entries were added etc., anytime after the tests (regardless if succeeded or failed).
Why not in-memory?
There are some in-memory DbContext providers, such as EFFORT. Why not use them? While I believe these are great and powerful tools (sure more mature than this project, so far), I ran into problems where an in-memory provider gave me false positives. At some point, I have introduced a new entity in my code-first app, and messed a FK relationship. Unit tests with Effort did not detect this change and kept on passing - and that is not what I wanted. I wanted to be sure that my entity relationship is not messed up by a simple bug - hence, this project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- EntityFramework (>= 6.1.3)
- NUnit (>= 3.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.32 | 1,080 | 9/24/2018 |
1.0.31 | 868 | 9/15/2018 |
1.0.30 | 840 | 9/15/2018 |
1.0.29 | 876 | 9/15/2018 |
1.0.28 | 1,031 | 6/14/2018 |
1.0.27 | 990 | 6/13/2018 |
1.0.26 | 963 | 6/13/2018 |
1.0.21 | 1,053 | 6/13/2018 |
1.0.20 | 1,009 | 6/13/2018 |
1.0.19 | 1,062 | 6/13/2018 |
1.0.18 | 970 | 6/13/2018 |
1.0.17 | 1,008 | 6/13/2018 |
1.0.16 | 989 | 6/13/2018 |
1.0.13 | 951 | 6/12/2018 |
1.0.12 | 929 | 6/12/2018 |
1.0.11 | 963 | 6/12/2018 |
1.0.10 | 961 | 6/12/2018 |
1.0.9 | 947 | 6/12/2018 |
1.0.8 | 892 | 6/12/2018 |
1.0.7 | 942 | 6/12/2018 |
1.0.6 | 914 | 6/12/2018 |
1.0.5 | 1,052 | 6/11/2018 |
1.0.4 | 976 | 6/11/2018 |
1.0.3 | 906 | 6/11/2018 |
1.0.2 | 984 | 6/11/2018 |