A component test verifies the functionality of individual components within an application by isolating and testing them in a controlled environment.
Common techniques used in C# component tests are:
- Mocking and stubbing: Creating simulated objects to replace dependencies and control their behavior.
- Dependency injection: Injecting dependencies into components to make them more testable.
- Test-driven development (TDD): Writing tests before writing the actual code, driving development in a test-first manner.
Component Test example
I’ve worked in teams where Integration Tests are re-used, the external dependencies are mocked out with another appsettings file. This is in my opinion then a Component Test
, so the two resources above HTTP
and Database
are mocked / stubbed.
The appsettings file could then be called something like appsettings.Mock.json
Using a fixture
It makes sense to break the tests down but still group them by Features
- these should be already grouped by your controllers. By design controllers are allowed to do too much, you can add as many endpoints in them as you like - thats a different topic, see MVC Controllers are Dinosaurs - Embrace API Endpoints. For my features examples however grouping responsability makes component testing by feature easier.
Consider these controllers, Artists
has simple CRUD operations and so does Songs
, these operations are abstracted away and then injected as an interface, using the Controllers
folder is simply following convention over configuration.
I would understand this as one Artist
has many Songs
.
1 | Foo.Api/Controllers/ArtistsController.cs |
The component tests could then be created as Features/Artists
and Features/Songs
, understandably there could be overlap if a Artist is persisted with some songs - ideally the link would just be the foreign key and not the whole implementation so design considerations need to thought out.
1 | Foo.Api.ComponentTests/Features/Artists/ |
The abstractions the controller instantiates though dependency injection would still be injected by the applications framework, their behavior also doesnt change however the configuration (appsettings in .Net) can be changed by environment. This means the application needs to have the same appsettings
values as the component tests.
Collection Fixtures
When to use: when you want to create a single test context and share it among tests in SEVERAL TEST CLASSES, and have it cleaned up after all the tests in the test classes have finished. - xunit.net/docs/shared-context
Examples of data it could share
Options
values (config)- Clients (classes that use HttpClient to call APIs)
The tests can use something called a Fixture
to instantiate their dependencys. One or more fixtures are then injected into our tests. So below the CollectionFixture
is really a base class to share context between tests.
Example Foo.Api.ComponentTests/Fixtures/CollectionFixture.cs
1 | /// <summary> |
We use IAsyncLifetime
to call into the services to dispose of resources. If you use InitializeAsync
here to seed any data you must remember its for all tests. If you want to seed data just for a subset then see Class Fixtures
below.
1 | public Task DisposeAsync() |
To then allow the CollectionFixture
to be instanciated and managed we need to create a SharedCollectionFixture
1 | [ ] |
Test classes that want to use CollectionFixture
should be decorated with [Collection("Shared Collection Fixture")]
. Their constructor parameters can then include CollectionFixture collectionFixture
. The facts can then access _collectionFixture.FooServiceClient
1 | [ ] |
FooServiceClient
The FooServiceClient
should inherit/implement IDisposable
to dispose of any clients it may have. This is called from the fixtures DisposeAsync
method to ensure all state is disposed of.
GC.SuppressFinalize(this)
will prevent derived types that introduce a finalizer from needing to re-implement IDisposable to call it. See CA1816: Call GC.SuppressFinalize correctly
Alternatively the client FooServiceClient
could use the sealed keyword to prevent inheritance.
1 | public class FooServiceClient : IDisposable |
Class Fixtures
When to use: when you want to create a single test context and share it among all the tests ONE TEST CLASS, and have it cleaned up after all the tests in the class have finished. - xunit.net/docs/shared-context
Examples of what it can do
- Seed data before each test, ie: call your API under test and seed data (this is more for an integration tests, it may not be sensible for component but I kept this point for completeness should you be reusing component as integration tests)
- Provide IDs (often GUIDs) for the seeded data, you would then use this in your tests
Create the fixture the same as above but it will not need the CollectionDefinition
class.
1 | /// <summary> |
Create the tests and inherit/implement IClassFixture<T>
and IAsyncLifetime
.
1 | public class FindStuffTests : IClassFixture<FindStuffFixture>, IAsyncLifetime |
Using Collection Fixtures And Class Fixtures together
1 | [ ] |