You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**EntityFrameworkCore.AutoFixture** is the logical product
10
-
of [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) in-memory providers
11
-
and [AutoFixture](https://github.com/AutoFixture/AutoFixture).
12
-
13
-
Using **EntityFrameworkCore.AutoFixture** you can greatly reduce the boilerplate work necessary to unit test code that
14
-
uses **Entity Framework Core** database contexts (see [examples](#examples)). You'll appreciate this library if you are
15
-
already using **AutoFixture** as your auto-mocking container.
16
-
17
-
**EntityFrameworkCore.AutoFixture** extens **AutoFixture** with the ability to create fully functional `DbContext`
18
-
instances, with very little setup code.
9
+
**EntityFrameworkCore.AutoFixture** is a library that helps with testing code that uses [Entity Framework](https://docs.microsoft.com/en-us/ef/core/), by reducing the boilerplate code necessary to set up database contexts (see [examples](#examples)), with the help of [AutoFixture](https://github.com/AutoFixture/AutoFixture).
19
10
20
11
Unlike other libraries for faking EF contexts, **EntityFrameworkCore.AutoFixture** does not use mocking frameworks or
21
-
dynamic proxies in order to create `DbContext` instances, instead it uses the Microsoft's own
22
-
in-memory [providers](https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/) for EF Core. This allows to make
23
-
less assumptions (read as: mock setups) in your tests about how the `DbContext` will behave in the real environment.
12
+
dynamic proxies in to create database contexts, instead it uses the actual database [providers](https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/). This ensures the tests will behave a lot more similar to the code in the production environment, with little to no effort.
24
13
25
14
#### :warning: .NET Standard 2.0 in EF Core v3.0.x :warning:
26
15
@@ -31,37 +20,28 @@ version of Entity Framework Core.
31
20
32
21
## Features
33
22
34
-
**EntityFrameworkCore.AutoFixture** offers three customizations to aid your unit testing workflow:
23
+
**EntityFrameworkCore.AutoFixture** offers three customizations to help your unit testing workflow:
35
24
36
-
-`InMemoryContextCustomization` - customizes fixtures to use the In-Memory database provider when creating *DbContext*
37
-
instances
38
-
-`SqliteContextCustomization` - customizes fixtures to use the SQLite database provider when creating *DbContext*
39
-
instances.
40
-
By default the customization will create contexts for an in-memory *connection string* (i.e. `DataSource=:memory:`).
41
-
This can be changed by providing the fixture a predefined `SqliteConnection` instance.
42
-
-`DbContextCustomization` - serves as the base customization for the other two implementations. The customization can
43
-
be used, in more advanced scenarios, when you want to extend the fixtures with your own specimen builders.
25
+
-`InMemoryCustomization` - Customizes fixtures to create contexts using the In-Memory database provider
26
+
-`SqliteCustomization` - Customizes fixtures to create contexts using the SQLite database provider
27
+
-`DbContextCustomization` - A base class for database provider customizations. Can be used, in more advanced scenarios, for example, to extend the existing functionality or create customizations for other providers.
44
28
45
29
## Examples
46
30
47
-
The examples below demonstrate, the possible ways of using the library in [xUnit](https://github.com/xunit/xunit) test
48
-
projects, both with `[Fact]` and `[Theory]` tests.
31
+
The examples below demonstrate, the possible ways of using the library in [xUnit](https://xunit.net) test projects, both with `[Fact]` and `[Theory]` tests.
49
32
50
-
The library is not limited to `xUnit` and can be used with other testing frameworks like `NUnit` and `MSTest`, since it
51
-
only provides a few `Customization` implementations.
33
+
The library is not limited to `xUnit` and can be used with other testing frameworks like [NUnit](https://nunit.org) and [MSTest](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest), since it only provides the customizations.
52
34
53
35
### Using In-Memory database provider
54
36
37
+
By default this customization will configure all contexts to use the [in-memory](https://docs.microsoft.com/en-us/ef/core/testing/testing-without-the-database#in-memory-provider) database provider for Enity Framework, and will create the database, giving you a ready to use context.
@@ -73,14 +53,25 @@ public async Task CanUseGeneratedContext()
73
53
}
74
54
```
75
55
76
-
The next example uses a custom `AutoData` attribute `AutoDomainDataWithInMemoryContext` that customizes the fixture with
77
-
the same customization as in the example above. This helps abstract away even more setup code.
56
+
With the default configuration, the custmization will set the store name to `TestDatabase` and will suffix it with a random string to ensure the name is unique. After the context is created it will run `Database.EnsureCreated()` to create the database.
57
+
58
+
This behavior can be changed by setting the corresponding values in the customizaiton initializer.
When using the SQLite database provider there is another configuration available in the customization,
112
-
called `AutoOpenConnection` which allows to automatically open database connections, after they are resolved from the
113
-
fixture. The option is turned off by default in v1, but might become the default in next major releases.
113
+
By default this customization will configure all contexts to use the [SQLite](https://docs.microsoft.com/en-us/ef/core/testing/testing-without-the-database#sqlite-in-memory) database provider for Enity Framework, and will automatically create the database, giving you a ready to use context.
@@ -135,38 +129,66 @@ public async Task CanUseGeneratedContext()
135
129
}
136
130
```
137
131
138
-
The same test can be written like this, by using a custom data attribute.
132
+
With the default configuration, the custmization will set the connection string to `Data Source=:memory:`, will open the connection and after the context is created it will run `Database.EnsureCreated()` to create the database.
133
+
134
+
This behavior can be changed by setting the corresponding values in the customizaiton initializer.
0 commit comments