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:
<ol><li>Create a test context using <xref href="Metalama.Testing.UnitTesting.UnitTestClass.CreateTestContext(System.String%2cSystem.String)" data-throw-if-not-resolved="false"></xref></li><li>Create a compilation using <xref href="Metalama.Testing.UnitTesting.TestContext.CreateCompilation(System.String%2cSystem.String%2cSystem.Boolean%2cSystem.Collections.Generic.IEnumerable%7bMicrosoft.CodeAnalysis.MetadataReference%7d%2cSystem.String%2cSystem.Boolean)" data-throw-if-not-resolved="false"></xref></li><li>Optionally switch execution context using <xref href="Metalama.Testing.UnitTesting.TestContext.WithExecutionContext(Metalama.Framework.Code.ICompilation%2cSystem.String)" data-throw-if-not-resolved="false"></xref> if your code uses <xref href="Metalama.Framework.Code.SyntaxBuilders.ExpressionFactory" data-throw-if-not-resolved="false"></xref></li><li>Query the code model or call your compile-time helper classes</li><li>Assert expected results</li></ol></p>
The test context must be disposed at the end of each test method.
Constructors
| Name | Description |
|---|---|
| UnitTestClass(ITestOutputHelper?, bool) | Initializes a new instance of the UnitTestClass class. |
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() |