Skip to content

Xunit Support Classes

Dennis C. Mitchell edited this page Apr 2, 2019 · 19 revisions

EDennis.AspNetCore.Base provides a number of abstract Xunit test classes and supporting classes for performing various kinds of Xunit integration tests. There are four broad categories of integration tests supported: (1) Repo Tests, which directly test repo class methods; (2) Controller Tests, which directly test controller methods (actions); (3) ApiClient Tests, which directly test ApiClient methods; and (4) Endpoint Tests, which test an API endpoint. Only the Endpoint Tests make use of the Microsoft's WebApplicationFactory. All other tests use special factory classes that do not derive from WebApplicationFactory.

Repo Tests

Four abstract classes provide support for testing repo methods. There is one abstract class for each of the four types of repos.

ReadonlyRepoTests Class

This class instantiates a ReadonlyRepo for use in directly testing the repo's public methods. The instantiated repo is exposed as a property called Repo.

Type Parameters

ReadonlyRepoTests has three type parameters.

Type Parameter Constraints
TRepo The type of the instantiate repo, which must extend ReadonlyRepo<TEntity,TContext>
TEntity TEntity is a class with a default constructor
TContext TContext extends Microsoft.EntityFrameworkCore.DbContext

Constructor

ReadonlyRepoTests has a single constructor with two arguments.

Parameter Description
ITestOutputHelper output The Xunit helper class for piping messages to the test output console
ConfigurationFactory<TRepo> fixture A class for finding the applicable appsettings.Development.json file and generating an IConfiguration from that file

Properties

ReadonlyRepoTests exposes two public properties.

Property Description
public TRepo Repo Provides direct access to the Repo object
public ITestOutputHelper Output The Xunit.Abstractions helper object for writing to the test output console

Example

public class TodoRepoTests : ReadonlyRepoTests<
                   TodoRepo, Todo, TodoContext> {
        public TodoRepoTests(ITestOutputHelper output, 
                ConfigurationFactory<TodoRepo> fixture)
            : base(output, fixture) { }
 // test methods...
}

Example

StateAgencyBackgroundCheckRepoTests

ReadonlyTemporalRepoTests Class

This class instantiates a ReadonlyTemporalRepo for use in directly testing the repo's public methods. The instantiated repo is exposed as a property called Repo.

Type Parameters

ReadonlyTemporalRepoTests has four type parameters.

Type Parameter Constraints
TRepo The type of the instantiate repo, which must extend ReadonlyRepo<TEntity,TContext>
TEntity TEntity is a class with a default constructor and implements IEFCoreTemporalModel
TContext TContext extends Microsoft.EntityFrameworkCore.DbContext (for current records)
THistoryContext THistoryContext extends Microsoft.EntityFrameworkCore.DbContext (for history records)

Constructor

ReadonlyTemporalRepoTests has a single constructor with two arguments.

Parameter Description
ITestOutputHelper output The Xunit helper class for piping messages to the test output console
ConfigurationFactory<TRepo> fixture A class for finding the applicable appsettings.Development.json file and generating an IConfiguration from that file

Properties

ReadonlyTemporalRepoTests exposes two public properties.

Property Description
public TRepo Repo Provides direct access to the Repo object
public ITestOutputHelper Output The Xunit.Abstractions helper object for writing to the test output console

Example

public class TodoRepoTests : ReadonlyTemporalRepoTests<
            TodoRepo, Todo, TodoContext, TodoHistoryContext> {
        public TodoRepoTests(ITestOutputHelper output, 
                ConfigurationFactory<TodoRepo> fixture)
            : base(output, fixture) { }
 // test methods...
}

Example

FederalAgencyBackgroundCheckRepoTests

WriteableRepoTests Class

(to be continued)

WriteableTemporalRepoTests Class

(to be continued)

Controller Tests

(to be continued)

TestRepoFactory Class

(to be continued)

TestApiClientFactory Class

(to be continued)

ApiClient Tests

(to be continued)

ReadonlyApiClientTests Class

(to be continued)

WriteableApiClientTests Class

(to be continued)

Endpoint Tests

(to be continued)

ReadonlyEndpointTests Class

(to be continued)

ReadonlyTemporalEndpointTests Class

(to be continued)

WriteableEndpointTests Class

(to be continued)

WriteableTemporalEndpointTests Class

(to be continued)

Other Factory and Support Classes

(to be continued)

ConfigurationFactory Class

(to be continued)

ConfiguringWebApplicationFactory Class

(to be continued)

ApiLauncherFactory Class

(to be continued)

TestHttpClientFactory Class

(to be continued)

ClassInfo Class

(to be continued)

Clone this wiki locally