- Install the xunit.di nuget package
- Create a Setup.cs class, (optional) and inherits the Xunit.Di.Setup.cs
- Configure dependencies in the Setup.cs class.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>project.tests</AssemblyName>
<RootNamespace>project.tests</RootNamespace>
<SetupNamespace>$(RootNamespace)</SetupNamespace>
<SetupAssembly>$(AssemblyName)</SetupAssembly>
<EnableXunitDi>true</EnableXunitDi>
...
</PropertyGroup>
...
</Project>
All these properties are optional, when not present, the default values would be used.
Configuration | Default | Values |
---|---|---|
AssemblyName | test project assembly name | The name of the final output assembly after the project is built. |
RootNamespace | test project namespace | The root namespace to use when you name an embedded resource. This namespace is part of the embedded resource manifest name. |
SetupNamespace | $(RootNamespace) | The namespace where you should find the Setup.cs class, set this value when using the Setup.cs from a different library project. |
SetupAssembly | $(AssemblyName) | The assembly where you should find the Setup.cs class, set this value when using the Setup.cs from a different library project. |
EnableXunitDi | true | Set it to false when you need to temporarily disable the xunit.di |
- Create a Setup.cs class in your test project. To leverage the setup provided by the package, you can optionally inherit the Xunit.Di.Setup.cs class.
public class Setup : Di.Setup
- Configure the dependency services
/// When inherits the Xunit.Di.Setup.cs class
protected override void Configure()
{
ConfigureAppConfiguration((hostingContext, config) =>
{
bool reloadOnChange =
hostingContext.Configuration.GetValue("hostBuilder:reloadConfigOnChange", true);
if (hostingContext.HostingEnvironment.IsDevelopment())
config.AddUserSecrets<Setup>(true, reloadOnChange);
});
ConfigureServices((context, services) =>
{
services.AddSingleton<TextReaderService>();
});
}
/// or otherwise configure the setup from scratch, and it needs to have a public property of type IServiceProvider. Refer to the Xunit.Di.Setup.cs class as a template.
- Consume the registered service from your test class.
private readonly TextReaderService _textReader;
public ServiceDependencyTests(TextReaderService textReader)
{
this._textReader = textReader;
}
[Fact]
public async Task Dependency_Instantiated_and_CanRead()
{
var value = await _textReader.Reader.ReadToEndAsync();
Assert.NotNull(value);
Assert.NotEmpty(value);
}
[^1]: The TextReaderService is a sample service class defined in the test project.
Find the sample usages from this test project Xunit.Di.Tests