{"id":21004921,"url":"https://github.com/aivascu/entityframeworkcore.autofixture","last_synced_at":"2025-05-15T01:33:12.263Z","repository":{"id":41461665,"uuid":"216630104","full_name":"aivascu/EntityFrameworkCore.AutoFixture","owner":"aivascu","description":"A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.","archived":false,"fork":false,"pushed_at":"2023-11-27T23:33:04.000Z","size":237,"stargazers_count":37,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-18T23:06:10.132Z","etag":null,"topics":["autofixture","database","entity-framework","entity-framework-core","in-memory","mocking","sqlite","testing","tests","unit-test","unit-testing"],"latest_commit_sha":null,"homepage":"https://aivascu.github.io/EntityFrameworkCore.AutoFixture/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aivascu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-21T17:48:35.000Z","updated_at":"2024-07-30T12:53:53.000Z","dependencies_parsed_at":"2024-11-19T08:38:56.450Z","dependency_job_id":"63c4f55c-3bab-4e2c-8453-33526bec0dd3","html_url":"https://github.com/aivascu/EntityFrameworkCore.AutoFixture","commit_stats":{"total_commits":138,"total_committers":5,"mean_commits":27.6,"dds":0.4565217391304348,"last_synced_commit":"d483899436b573e227233bd990cc126a77b45227"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aivascu%2FEntityFrameworkCore.AutoFixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aivascu%2FEntityFrameworkCore.AutoFixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aivascu%2FEntityFrameworkCore.AutoFixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aivascu%2FEntityFrameworkCore.AutoFixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aivascu","download_url":"https://codeload.github.com/aivascu/EntityFrameworkCore.AutoFixture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254256509,"owners_count":22040306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autofixture","database","entity-framework","entity-framework-core","in-memory","mocking","sqlite","testing","tests","unit-test","unit-testing"],"created_at":"2024-11-19T08:38:27.192Z","updated_at":"2025-05-15T01:33:11.948Z","avatar_url":"https://github.com/aivascu.png","language":"C#","readme":"# EntityFrameworkCore.AutoFixture\n\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/aivascu/EntityFrameworkCore.AutoFixture/release.yml?logo=github\u0026style=flat-square)\n[![Coveralls github](https://img.shields.io/coveralls/github/aivascu/EntityFrameworkCore.AutoFixture?logo=coveralls\u0026style=flat-square)](https://coveralls.io/github/aivascu/EntityFrameworkCore.AutoFixture?branch=master)\n[![NuGet](https://img.shields.io/nuget/v/EntityFrameworkCore.AutoFixture?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/EntityFrameworkCore.AutoFixture/)\n[![NuGet downloads](https://img.shields.io/nuget/dt/EntityFrameworkCore.AutoFixture?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/EntityFrameworkCore.AutoFixture/)\n[![GitHub](https://img.shields.io/github/license/aivascu/EntityFrameworkCore.AutoFixture?logo=MIT\u0026style=flat-square)](https://licenses.nuget.org/MIT)\n\n**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).\n\nUnlike other libraries for faking EF contexts, **EntityFrameworkCore.AutoFixture** does not use mocking frameworks or\ndynamic 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.\n\n### Supported versions\n\nThe current implementation supports all `Microsoft.EntityFrameworkCore` packages starting from `v2.0.0` and `AutoFixture` starting from `v4.4.0`, however because the constraints are so low you will have to install the latest versiosn of `Microsoft.EntityFrameworkCore` and `AutoFixture` packages yourself, alongside this package.\n\nMake sure to keep in sync the versions of packages used in production and test code. Meaning that if you use `Microsoft.EntityFrameworkCore.SqlServer` of version `v7.0.14`, you need to use the equivalent version of `Microsoft.EntityFrameworkCore.Sqlite`.\n\n| Integration | Package                                |\n|-------------|----------------------------------------|\n| SQLite      | Microsoft.EntityFrameworkCore.Sqlite   |\n| In-Memory   | Microsoft.EntityFrameworkCore.InMemory |\n\n#### :warning: .NET Standard 2.0 in EF Core v3.0.x :warning:\n\nEntity Framework Core `v3.0.0` - `v3.0.3` are targeting `netstandard2.1`, which means they are not compatible with\ntarget frameworks that support at most `netstandard2.0` (`\u003e= net47` and `netcoreapp2.1`).\nVersions after `v3.1` are targeting `netstandard2.0`. If you've encountered this issue consider upgrading to a later\nversion of Entity Framework Core.\n\n#### :warning: EF Core v7.0.x :warning:\n\nIf you're using EF Core v7 you must install the corresponding database provider package alongside this library.\u003cbr/\u003e\nSo for example if you're using SQLite, you must install `Microsoft.EntityFrameworkCore.Sqlite v7.0.0` or later, otherwise your tests will not pass.\n\n## Features\n\n**EntityFrameworkCore.AutoFixture** offers three customizations to help your unit testing workflow:\n\n- `InMemoryCustomization` - Customizes fixtures to create contexts using the In-Memory database provider\n- `SqliteCustomization` - Customizes fixtures to create contexts using the SQLite database provider\n- `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.\n\n## Examples\n\nThe examples below demonstrate, the possible ways of using the library in [xUnit](https://xunit.net) test projects, both with `[Fact]` and `[Theory]` tests.\n\nThe 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.\n\n### Using In-Memory database provider\n\nBy 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.\n\n```csharp\n[Fact]\npublic async Task CanSavesCustomers()\n{\n    // Arrange\n    var fixture = new Fixture().Customize(new InMemoryCustomization());\n    var context = fixture.Create\u003cTestDbContext\u003e();\n\n    // Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\nWith 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.\n\nThis behavior can be changed by setting the corresponding values in the customizaiton initializer.\n\n```csharp\n[Fact]\npublic async Task CanSavesCustomers()\n{\n    // Arrange\n    var fixture = new Fixture().Customize(new InMemoryCustomization\n    {\n        DatabaseName = \"MyCoolDatabase\", // Sets the store name to \"MyCoolDatabase\"\n        UseUniqueNames = false, // No suffix for store names. All contexts will connect to same store\n        OnCreate = OnCreateAction.Migrate // Will run Database.Migrate()\n                                          // Use OnCreateAction.None to skip creating the database\n    });\n    var context = fixture.Create\u003cTestDbContext\u003e();\n\n    // Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\nTo encapsulate the configuration and remove even more of the boilerplate, use the `AutoData` attributes offered by AutoFixture.\n\n```csharp\npublic class PersistenceDataAttribute : AutoDataAttribute\n{\n    public PersistenceDataAttribute()\n        : base(() =\u003e new Fixture().Customize(new InMemoryCustomization {\n            UseUniqueNames = false,\n            OnCreate = OnCreateAction.Migrate\n        }))\n    {\n    }\n}\n```\n\n```csharp\n[Theory, PersistenceData] // Notice the data attribute\npublic async Task CanUseGeneratedContext(TestDbContext context)\n{\n    // Arrange \u0026 Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\n### Using SQLite database provider\n\nBy 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.\n\n```csharp\n[Fact]\npublic async Task CanUseGeneratedContext()\n{\n    // Arrange\n    var fixture = new Fixture().Customize(new SqliteCustomization());\n    var context = fixture.Create\u003cTestDbContext\u003e();\n\n    // Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\nWith 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.\n\nThis behavior can be changed by setting the corresponding values in the customizaiton initializer.\n\n```csharp\n[Fact]\npublic async Task CanSavesCustomers()\n{\n    // Arrange\n    var fixture = new Fixture().Customize(new SqliteCustomization\n    {\n        ConnectionString = \"Data Source=MyDatabase.sqlite;Cache=Shared;\", // Sets the connection string to connect to a file\n        AutoOpenConnection = false, // Disables opening the connection by default. Affects all SqliteConnection instances.\n        OnCreate = OnCreateAction.None // Use OnCreateAction.None to skip creating the database.\n                                       // Use OnCreateAction.EnsureCreated to run Database.EnsureCreated() automatically\n                                       // Use OnCreateAction.Migrate to run Database.Migrate() automatically\n    });\n    var connection = fixture.Freeze\u003cSqliteConnection\u003e();\n    var context = fixture.Create\u003cTestDbContext\u003e();\n    connection.Open();\n    context.Database.Migrate();\n\n    // Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\nTo encapsulate the configuration and remove even more of the boilerplate, use the `AutoData` attributes offered by AutoFixture.\n\n```csharp\npublic class PersistenceDataAttribute : AutoDataAttribute\n{\n    public PersistenceDataAttribute()\n        : base(() =\u003e new Fixture().Customize(new SqliteCustomization {\n            ConnectionString = \"Data Source=MyDatabase;Mode=Memory;Cache=Shared;\"\n            OnCreate = OnCreateAction.Migrate\n        }))\n    {\n    }\n}\n```\n\n```csharp\n[Theory, PersistenceData] // Notice the data attribute\npublic async Task CanUseGeneratedContext(TestDbContext context)\n{\n    // Arrange \u0026 Act\n    context.Customers.Add(new Customer(\"Jane Smith\"));\n    await context.SaveChangesAsync();\n\n    // Assert\n    context.Customers.Should().Contain(x =\u003e x.Name == \"Jane Smith\");\n}\n```\n\n## License\n\nCopyright \u0026copy; 2019 [Andrei Ivascu](https://github.com/aivascu).\u003cbr/\u003e\nThis project is [MIT](https://github.com/aivascu/EntityFrameworkCore.AutoFixture/blob/master/LICENSE) licensed.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faivascu%2Fentityframeworkcore.autofixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faivascu%2Fentityframeworkcore.autofixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faivascu%2Fentityframeworkcore.autofixture/lists"}