A base class for xUnit tests that need to unit-test compile-time helper classes or query the Metalama code model.
Implements
Namespace: Metalama.Testing.UnitTesting
Assembly: Metalama.Testing.UnitTesting.dll
Syntax
public abstract class UnitTestClass : IDisposableRemarks
This class provides the infrastructure for writing unit tests of compile-time logic used by aspects, without executing the aspects themselves. Test methods should call CreateTestContext(string?, string?) to obtain a TestContext that provides access to Metalama services.
A typical test workflow is:
- Create a test context using CreateTestContext(string?, string?)
- Create a compilation using CreateCompilation(string, string?, bool, IEnumerable<MetadataReference>?, string?, bool)
- Optionally switch execution context using WithExecutionContext(ICompilation, string?) if your code uses ExpressionFactory
- Query the code model or call your compile-time helper classes
- Assert expected results
The test context must be disposed at the end of each test method.
Constructors
| Name | Description |
|---|---|
| UnitTestClass(ITestOutputHelper?, bool) |
Properties
| Name | Description |
|---|---|
| TestOutput | Gets an object allowing to write to the test output. |
Methods
| Name | Description |
|---|---|
| AddExceptionHandler(IAdditionalServiceCollection) | |
| AddSyntaxGenerationOptions(IAdditionalServiceCollection) | |
| ConfigureExtensions(ITestExtensionCollector) | |
| ConfigureServices(IAdditionalServiceCollection) | Adds services or mocks that are common to all tests in the current class. This method is called
by CreateTestContext(string?, string?) and the
|
| CreateAdditionalServiceCollection(params IService[]) | Creates a collection of additional services that can then be passed to CreateTestContext(IAdditionalServiceCollection, string?, string?). |
| CreateDefaultTestContextOptions() | |
| CreateTestContext(IAdditionalServiceCollection, string?, string?) | Creates a test context with a collection of additional services or mocks. |
| CreateTestContext(TestContextOptions?, IAdditionalServiceCollection?, string?, string?) | Creates a test context, optionally with a non-default TestContextOptions or a collection of additional services or mocks. |
| CreateTestContext(string?, string?) | |
| CreateTestContextCore(TestContextOptions, IAdditionalServiceCollection) | |
| CreateTestFaultInjector() | Creates the Metalama.Testing.Hooks.TestFaultInjector of each test context created by this class. A derived class can override this method to supply a differently configured injector, for instance one with a fault already armed. |
| CreateTestSynchronizationProvider() | Creates the Metalama.Testing.Hooks.TestSynchronizationProvider of each test context created by this class. A derived class can override this method to supply a differently configured provider. |
| Dispose() |