Giter Site home page Giter Site logo

calzolari-efcore-flurl-fakebearertoken-testserver's Introduction

Clean integration tests with Calzolari.TestServer.EntityFramework

A fluent library to easily write integration tests for ASP.NET Core with EntityFramework Core.

Following example uses AutoFixture and xUnit but you can use other tools if you desire.

Parallelism MUST BE deactivated while testing, the database is created and removed after each test which avoids any data collision, the DbContext is reinstancianted between each test.

It's not recommended for large databases.

This library uses Flurl and FakeBearerToken

Steps to setup your project

Create your integration test project

Optionally you can add:

xUnit as tests runner

FluentAssertions.Web for assertions on HttpResponseMessage

AutoFixture for autofill any type of object

Install the package Calzolari.TestServer.EntityFramework

The package can be found here: https://www.nuget.org/packages/Calzolari.TestServer.EntityFramework/

The version 5.0.5 is the current version (.NET 5), a .NET Core 3.1 is coming soon

Create a dedicated Startup file for your integration tests

Add the method AddApplicationPart which takes in parameter any class of your webapplication, this is needed to retrieve controllers and register them in the integration test project like this:

services.AddControllers().AddApplicationPart(typeof(Startup).Assembly);

Then register The DbContext with the following method: AddIntegrationTestDbContext, this is needed for integration test to be working with EF Core, behind the scene it register your DbContext with SQLite Database:

services.AddIntegrationTestDbContext<DemoDbContext>()

Finally register the fake bearer token authentication like this:

services.AddFakeBearerToken();

This is needed to create an identity with a fake token during the tests

Create a Web factory

Inherit from FlurlWebFactory, generic T can be any class of your webapplication, then override CreateHostBuilder and use your integration Startup file like this:

using Calzolari.TestServer.EntityFramework.Flurl;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Calzolari.WebApi.Tests.Common
{
    public class DemoFactory : FlurlWebFactory<Startup>
    {
        protected override IHostBuilder CreateHostBuilder()
        {
            return Host.CreateDefaultBuilder().ConfigureWebHost((builder) =>
            {
                builder.UseStartup<TestStartup>();
            });
        }
    }
}

Create a xUnit collection definition

A collection definition allows injection dependency (Singleton lifetime) within your test classes. The following example shows how to register a collection named "AssemblyFixture" that allows to pass into your test classes the webfactory named "DemoFactory":

using Xunit;

namespace Calzolari.WebApi.Tests.Common
{
    [CollectionDefinition("AssemblyFixture")]
    public class AssemblyFixture : ICollectionFixture<DemoFactory> { }
}

Create a base test class

Inherit from IntegrationTestBase<TDbContext, T>, TDbContext is your DbContext and T is the same class that you defined as generic parameter on FlurlWebFactory Add [Collection("AssemblyFixture")] attribute on your base test class. This is here where you can add AutoFixture or anything else that you need for your test. Your base test class should look like this:

using AutoFixture;
using Calzolari.TestServer.EntityFramework.Database.EF;
using Calzolari.WebApi.Database;
using Xunit;

namespace Calzolari.WebApi.Tests.Common
{
    [Collection("AssemblyFixture")]
    public class DemoTestBase : IntegrationTestBase<DemoDbContext, Startup>
    {
        protected readonly IFixture Fixture;

        public DemoTestBase(DemoFactory factory) : base(factory)
        {
            Fixture = new Fixture();
        }
    }
}

Write your tests

Create a test class and inherit from the base test class. Pass the web factory by injection dependency and write your tests. I suggest to use FluentAssertions for your assertions

To feed the database use the method Arrange like this:

var countries = Fixture.Build<Country>()
                       .CreateMany(3);
  
Arrange(dbContext =>
{
    dbContext.Countries.AddRange(countries);
});

If you expect an auto incremented Id it works like if you were using EntityFramework within your application:

var country = Fixture.Create<Country>();

Arrange(dbContext => { dbContext.Countries.Add(country); });

// country.CountryId CountryId is filled

Then call your System Under Test (SUT):

var response = await BASE_REQUEST.Route(BaseRoute).GetAsync();

And assert the result:

response.ResponseMessage
	.Should()
	.Be200Ok()
	.And
	.BeAs(countries);

Simple usage of GET verb can be found here: https://github.com/AnthonyGiretti/Calzolari-EFCore-Flurl-FakeBearerToken-TestServer/blob/main/Calzolari.WebApi.Tests/CountryControllerTests/GetByIdTests.cs

Simple usage of POST verb can be found here with the usage of the fake bearer token: https://github.com/AnthonyGiretti/Calzolari-EFCore-Flurl-FakeBearerToken-TestServer/blob/main/Calzolari.WebApi.Tests/CountryControllerTests/PostTests.cs

calzolari-efcore-flurl-fakebearertoken-testserver's People

Contributors

anthonygiretti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.