{"id":29179974,"url":"https://github.com/canerpatir/commonfixtures","last_synced_at":"2025-07-01T19:07:42.353Z","repository":{"id":116534178,"uuid":"267353335","full_name":"CanerPatir/CommonFixtures","owner":"CanerPatir","description":"A toolkit contains essential test fixtures for .net core and asp.net core projects.","archived":false,"fork":false,"pushed_at":"2022-03-27T09:06:10.000Z","size":86,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-28T22:34:40.932Z","etag":null,"topics":["aspnet-core","nunit","selenium","tdd","unit-testing","xunit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CanerPatir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-05-27T15:14:38.000Z","updated_at":"2022-03-27T09:01:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"62dece48-7d42-4ad8-a6fa-a40826858a7a","html_url":"https://github.com/CanerPatir/CommonFixtures","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CanerPatir/CommonFixtures","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CanerPatir%2FCommonFixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CanerPatir%2FCommonFixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CanerPatir%2FCommonFixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CanerPatir%2FCommonFixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CanerPatir","download_url":"https://codeload.github.com/CanerPatir/CommonFixtures/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CanerPatir%2FCommonFixtures/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263021823,"owners_count":23401148,"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":["aspnet-core","nunit","selenium","tdd","unit-testing","xunit"],"created_at":"2025-07-01T19:07:41.348Z","updated_at":"2025-07-01T19:07:42.263Z","avatar_url":"https://github.com/CanerPatir.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"CommonFixtures\n========\n\n![GitHub Workflow Status](https://github.com/CanerPatir/CommonFixtures/workflows/Nuget%20Package%20Deploy/badge.svg)\n[![codecov](https://codecov.io/gh/CanerPatir/CommonFixtures/branch/master/graph/badge.svg?token=OSKYBSA9KW)](https://img.shields.io/codecov/c/github/CanerPatir/CommonFixtures?token=OSKYBSA9KW)\n![Nuget](https://img.shields.io/nuget/v/CommonFixtures?color=blue\u0026logo=nuget)\n\nCommon fixture is a toolkit contains essential test fixtures for .net core and asp.net core test projects. CommonFixtures aims \nto reduce your preparing test suite effort for common testing concerns and supplying ready to use testing infrastructure.\nCommonFixtures can be used independently from testing framework. You can prefer any kind of dotNet testing framework like xUnit, nUnit etc.\n\n|Supported .Net Core Versions|\n|--------------|\n|.netocreapp3.1|\n|.netocreapp3.0|\n\n\nReady to use fixtures\n========\nCommonFixtures supplies some base classes to supply underlying fixtures for your test suite. These are as below;\n    \n* [BaseTest](#basetest)\n* [WithIoC](#withioc)\n* [WithHost](#withhost)\n* [WithWebApp](#withwebapp)\n* [WithEfCore](#withefcore)\n* [WithWebAppAndEfCore](#withwebappandefcore)\n* Selenium support for WithWebApp and WithWebAppAndEfCore fixtures\n\n## BaseTest \n\nContains basic mocking and stubbing methods to supply standard DSL for all tests. (It uses FakeItEasy library for faking and AutoFixture for data generation internally)\n\n* Stubbing demo\n```csharp\npublic class ProductServiceTests : BaseTest\n{\n    private readonly ProductService _sut;\n    private readonly IProductRepository _mockRepository;\n\n    public ProductServiceTests()\n    {\n        _mockRepository = Mock\u003cIProductRepository\u003e();\n        _sut = new ProductService(_mockRepository);\n    }\n\n    // with your favourite testing tool xUnit, nunit ...  etc.\n    // [Fact]\n    // [Test]\n    public async Task Should_Created_Product()\n    {\n        // Given\n        var givenTitle = Random\u003cstring\u003e();\n        var givenPrice = Random\u003cdecimal\u003e();\n        var expectedId = Random\u003cint\u003e();\n        \n        StubAsync(() =\u003e _mockRepository.CreateProduct(ArgMatches\u003cProduct\u003e(x =\u003e x.Title == givenTitle \u0026\u0026 x.Price == givenPrice), ArgIgnore\u003cCancellationToken\u003e()), expectedId);\n\n        // When\n        var id =  await _sut.CreateProduct(givenTitle, givenPrice);\n\n        // Then\n        Assert.Equal(expectedId, id);\n    }\n}\n```\n* Verification demo\n```csharp\npublic async Task Should_Call_Repo_Once()\n{\n    // Given\n    var givenTitle = Random\u003cstring\u003e();\n    var givenPrice = Random\u003cdecimal\u003e();\n    \n    // When\n    await _sut.CreateProduct(givenTitle, givenPrice);\n\n    // Then\n    Verify(() =\u003e _mockRepository.CreateProduct(ArgMatches\u003cProduct\u003e(x =\u003e x.Title == givenTitle \u0026\u0026 x.Price == givenPrice), ArgIgnore\u003cCancellationToken\u003e()), numberOfTimes: 1);\n}\n```\n\n## WithIoC \n\nServiceCollection fixture to keep your IoC logic afloat throughout the test. \n\n```csharp\npublic class ProductServiceTest : WithIoC\n{\n    protected override void ConfigureServices(IServiceCollection services)\n    {\n        services.MockAndRegister\u003cIProductRepository\u003e();\n        services.Register\u003cProductService\u003e();\n    }\n    \n    [Fact]\n    public async Task Should_Created_Product()\n    {\n        // Given\n        var givenTitle = Random\u003cstring\u003e();\n        var givenPrice = Random\u003cdecimal\u003e();\n        var mockRepository = GetService\u003cIProductRepository\u003e();\n        var sut = GetService\u003cProductService\u003e();\n        \n        // When\n        await sut.CreateProduct(givenTitle, givenPrice);\n\n        // Then\n        Verify(() =\u003e mockRepository.CreateProduct(ArgMatches\u003cProduct\u003e(x =\u003e x.Title == givenTitle \u0026\u0026 x.Price == givenPrice), ArgIgnore\u003cCancellationToken\u003e()), numberOfTimes: 1);\n    }\n}\n```\n\n* Also allows you to mock some dependencies of the system under test while leaving others as they are\n```csharp\npublic class DependencyMockingTest : WithIoC\n{ \n    public class YourAwesomeService\n    {\n         public YourAwesomeService(IDependency1 dep1, IDependency2 dep2)\n         {\n            _dep1 = dep1;\n            _dep2 = dep2;\n         }\n\n         // ....\n    }\n\n    protected override void ConfigureServices(IServiceCollection services)\n    {\n        services.MockAndRegister\u003cIDependency1\u003e();\n        services.Register\u003cYourAwesomeService\u003e();\n        // so mocked just dependency 1 and leaving second one as it is\n    }\n\n    // ....\n}\n```\n\n## WithHost\n\nSupplies test host for testing .net core hosted services (see: [.NET Core Hosted Services](https://docs.microsoft.com/tr-tr/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1\u0026tabs=visual-studio))\n\n```csharp\npublic class QueuedJobHostedServiceTests : WithHost\n{\n    protected override void ConfigureServices(IServiceCollection services)\n    {\n        // adding your awesome hosted service\n        services.AddHostedService\u003cQueuedJobHostedService\u003e();\n    }\n    \n    [Fact]\n    public async Task It_Should_Execute_Queued_Task()\n    {\n        // Arrange\n        var queueManager = GetService\u003cQueueManager\u003e();\n        var counter = 0;\n        // Act\n        queueManager.EnqueueJob(() =\u003e\n        {\n            Interlocked.Increment(ref counter);\n        });\n        \n        // Assert\n        await Task.Delay(300);\n        Assert.Equal(1, counter);\n    }\n}\n```\n\n## WithWebApp \n\nAspnet core test server fixture for integration testing scenarios\n\n```csharp\npublic class MvcIntegrationTest : WithWebApp\u003cStartup\u003e\n{\n    protected override void ConfigureServices(IServiceCollection services)\n    {        \n        // You can override or mock services that registered from Startup.cs before\n    }\n\n    [Fact]\n    public async Task Get_EndpointsReturnSuccessAndCorrectContentType()\n    {\n        // Arrange\n        var client = HttpClient;\n\n        // Act\n        var response = await client.GetAsync(\"/weatherforecast\");\n\n        // Assert\n        response.EnsureSuccessStatusCode(); \n        Assert.Equal(\"application/json; charset=utf-8\", \n            response.Content.Headers.ContentType.ToString());\n    }\n}\n```\n\u003e :warning: Ensure _Microsoft.AspNetCore.Mvc.Testing_ package added to root of your test project before using this fixture.\n\n## WithEfCore \n\n* Supplies test fixture to test projects which use Entity Framework Core as persistence layer. \n* CommonFixtures automatically replaces your existing ef core db context configuration with in memory sqlLite. \n* Provides useful approach when you want to test your persistence logic like db model, entity validations, secend level cache, repository abstractions without mocking anything.\n\n```csharp\npublic class ProductRepositoryTest : WithEfCore\u003cApplicationDbContext\u003e\n{\n    [Fact]\n    public async Task Should_Persist_New_Created_Product()\n    {\n        // Arrange\n        Arrange(dbContext =\u003e\n        {\n            // helper that pull the database to desired state before acting (optional)\n        });\n\n        var sut = new ProductRepository(DbContext);\n\n        // Act\n\n        var id = await sut.CreateProduct(Random\u003cProduct\u003e(), CancellationToken.None);\n\n        // Assert\n        NewServiceScope(); // dispose all of services and recreate new one to simulate new scope like new http request\n        Assert.IsType\u003cint\u003e(id);\n        Assert.NotEqual(default, id);\n        var createdProduct = Get\u003cProduct\u003e(id);\n        Assert.NotNull(createdProduct);\n    }\n}\n```\n\n## WithWebAppAndEfCore \n\n* Combination of WithWebApp and WithEfCore\n* In addition to the _WithEfCore_, starts test server up and running then replace db context which registered in the Startup.ConfigureServices method\n* It is useful when you want to make test more complex application logic without mocking persistence layer\n\n```csharp\n// Automatically replaces your db context configuration specified in Startup.cs with in memory sqlLite\npublic class CreateProductCommandHandlerTest : WithWebAppAndEfCore\u003cStartup, ApplicationDbContext\u003e\n{\n    [Fact]\n    public async Task Should_Created_New_Product()\n    {\n        // Arrange\n        Arrange(dbContext =\u003e\n        {\n            // you can use pull the database to desired state before acting\n            // dbContext.Categories.Add(Random\u003cCategory\u003e());\n        });\n        \n        var mediator = GetService\u003cIMediator\u003e();\n\n        // Act\n        var id = await mediator.Send(Random\u003cCreateProductCommand\u003e());\n\n        // Assert\n        NewServiceScope(); // dispose all of services and recreate new one to simulate new scope like new http request\n        Assert.IsType\u003cint\u003e(id);\n        Assert.NotNull(Get\u003cProduct\u003e(id));\n    }\n}\n```\n## UI Testing With Selenium\n* Make sure that following two dependencies are added to your test project.\n\n```xml\n  \u003cPackageReference Include=\"Selenium.WebDriver\" Version=\"3.141.0\" /\u003e\n  \u003cPackageReference Include=\"Selenium.Chrome.WebDriver\" Version=\"83.0.0\" /\u003e\n``` \n\n```csharp\n// see SampleWebApp Index.cshtml to see sut \npublic class MvcViewIntegrationTest : WithWebApp\u003cStartup\u003e\n{\n    protected override bool SeleniumEnabled =\u003e true;\n    protected override bool SeleniumHeadless =\u003e true; // you can make false to see chrome browser\n\n    [Fact]\n    public void Counter_Test()\n    {\n        // Arrange\n        WebDriverWait waitForElement = new WebDriverWait(Selenium, TimeSpan.FromSeconds(10));\n        waitForElement.Until(ElementIsVisible(By.Id(\"counter\")));\n        \n        var button = Selenium.FindElement(By.Id(\"btn\"));\n        var counterSpan = Selenium.FindElement(By.Id(\"counter\"));\n        \n        // Act\n        Assert.Equal(0, int.Parse(counterSpan.Text));\n        button.Click();\n    \n        // Assert\n        counterSpan = Selenium.FindElement(By.Id(\"counter\"));\n        Assert.Equal(1, int.Parse(counterSpan.Text));\n    }\n}\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanerpatir%2Fcommonfixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcanerpatir%2Fcommonfixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanerpatir%2Fcommonfixtures/lists"}