{"id":16388743,"url":"https://github.com/timostamm/datafixtures","last_synced_at":"2026-04-17T13:32:39.392Z","repository":{"id":66186604,"uuid":"104890013","full_name":"timostamm/Datafixtures","owner":"timostamm","description":".NET Datafixtures for Unit Tests","archived":false,"fork":false,"pushed_at":"2017-09-27T07:28:47.000Z","size":7881,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-03T10:24:27.757Z","etag":null,"topics":["dotnet","entityframework","fixture-loader","fixtures","unittest"],"latest_commit_sha":null,"homepage":null,"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/timostamm.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":"2017-09-26T13:43:48.000Z","updated_at":"2017-09-26T17:08:44.000Z","dependencies_parsed_at":"2023-02-21T17:00:23.418Z","dependency_job_id":null,"html_url":"https://github.com/timostamm/Datafixtures","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timostamm%2FDatafixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timostamm%2FDatafixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timostamm%2FDatafixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timostamm%2FDatafixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timostamm","download_url":"https://codeload.github.com/timostamm/Datafixtures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240199172,"owners_count":19763820,"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":["dotnet","entityframework","fixture-loader","fixtures","unittest"],"created_at":"2024-10-11T04:29:48.385Z","updated_at":"2025-10-17T12:33:50.624Z","avatar_url":"https://github.com/timostamm.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Datafixtures\n.NET Datafixtures for Unit Tests\n\nTest fixtures can be used to setup a certain environment state - usually a database. They make unit testing easier because you can easily enforce a particular state neccessary to test your code. \nFixtures should be able to depend on each other, so that each fixture can focus on its own data.\n\nThis library provides a Fixture base class and a fixture loader that takes care of dependencies. \n\n\n##### Defining and loading simple fixtures\n\nA simple fixture extends `Fixture\u003cT\u003e` and overrides `DoLoad()`:\n\n```C#\nclass StartOfUnixTime : Fixture\u003cDateTime\u003e\n{\n   protected override DateTime DoLoad()\n    {\n        return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);\n    }\n}\n```\n\nTo use this fixture, you need to load it:\n\n```C#\nvar loader = new FixtureLoader();\nvar unixStart = loader.Add\u003cStartOfUnixTime\u003e();\nloader.Load();\n\n// The data generated by the fixture is available via the property \"Result\".\nAssert.AreEqual(1970, unixStart.Result.Year);\n```\n\nTo make a fixture depend on another fixture, simply reference it as a constructor argument.\n\n```C#\nclass UnixStartMessage : Fixture\u003cstring\u003e\n{\n\n    private readonly StartOfUnixTime start;\n\n    public UnixStartMessage(StartOfUnixTime start)\n    {\n        this.start = start;\n    }\n\n    protected override string DoLoad()\n    {\n        // You can access the result of the StartOfUnixTime \n        // fixture via its Result property\n        return \"hello world, it is \" + start.Result.Year;\n    }\n}\n```\n\nWhen loading this fixture, the dependant fixtures are loaded automatically. You just need to load the fixture you are actually using.\n\n```C#\nvar loader = new FixtureLoader();\nvar msg = loader.Add\u003cUnixStartMessage\u003e();\nloader.Load();\nAssert.AreEqual(\"hello world, it is 1970\", msg.Result);\n```\n\n##### Using fixtures with the EntityFramework\n\nIn a fixture, you probably want to add an entity to your db context and save the changes. The db context is just another dependency and can simply be injected as a constructor argument.\n\n```C#\nclass FirstStudent : Fixture\u003cStudent\u003e\n{\n\n    private readonly SchoolDbContext schoolDb;\n    \n    public UnixStartMessage(SchoolDbContext schoolDb)\n    {\n        this.schoolDb = schoolDb;\n    }\n\n    protected override Student DoLoad()\n    {\n        var first = new Student()\n        {\n            StudentName = \"Donald Knuth\" \n        };\n        schoolDb.Students.AddObject( first );\n        return first;\n    }\n}\n```\n\n##### Clearing and initializing the database\n\nLoading Database fixtures has a few implications. If you want your database to be in a repeatable state, you have to empty it first. You can use the utility class EntityFixtures to drop the entire database, initialize it, and load your fixtures:\n\n```C#\nvar schoolDb = new SchoolDbContext();\nFixtureLoader loader = EntityFixtures.DropCreateDatabase(scoolDb)\nvar first = loader.add\u003cFirstStudent\u003e();\nloader.load();\n\n// now you have a clean database with the first student\nSystem.Diagnostics.Trace.WriteLine(\"First student: \" + first.Result.StudentName); // =\u003e \"First student: Donald Knuth\"\n```\n\n##### Drop the database ?!?\n\nYou should use a LocalDB for your unittests. `EntityFixtures.DropCreateDatabase()` actually throws an exception if you do not, but in any case: use your own brain.\n\n\n##### Injecting other dependencies\nIf you have a look at `EntityFixtures.DropCreateDatabase()`, it just calls `FixtureLoader.RegisterService()` with your DbContext, and thereby making it available for constructor injection:\n```C#\nloader.RegisterService\u003cTContext\u003e(context);\n```\nYou are of course free to register and use other dependencies.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimostamm%2Fdatafixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimostamm%2Fdatafixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimostamm%2Fdatafixtures/lists"}