Skip to content

Make it work with .NET 6 to .NET 9 and use min versions of dependent packages #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
![Nuget](https://img.shields.io/nuget/v/Xunit.Microsoft.DependencyInjection)
![Nuget](https://img.shields.io/nuget/dt/Xunit.Microsoft.DependencyInjection)

# Xunit Dependency Injection framework - .NET 9.0
# Xunit Dependency Injection framework

Xunit does not support any built-in dependency injection features, therefore developers have to come up with a solution to recruit their favourite dependency injection framework in their tests.
Xunit does not support any built-in dependency injection features, therefore developers have had to come up with a solution to recruit their favourite dependency injection framework in their tests.

This library brings in Microsoft's dependency injection container to Xunit by leveraging Xunit's fixture.
This library brings Microsoft's dependency injection container to Xunit by leveraging Xunit's fixture.

## Getting started

Expand All @@ -20,7 +20,7 @@ Install-Package Xunit.Microsoft.DependencyInjection

### Setup your fixture

The abstract class of `Xunit.Microsoft.DependencyInjection.Abstracts.TestBedFixture` contains the necessary functionalities to add services and configurations to Microsoft's dependency injection container. Your concrete test fixture class must derive from this abstract class and implement the following two abstract methods:
The abstract class of `Xunit.Microsoft.DependencyInjection.Abstracts.TestBedFixture` contains the necessary functionality to add services and configurations to Microsoft's dependency injection container. Your concrete test fixture class must derive from this abstract class and implement the following two abstract methods:

```csharp
protected abstract IEnumerable<string> GetConfigurationFiles();
Expand Down Expand Up @@ -52,7 +52,7 @@ To access async scopes simply call the following method in the abstract fixture
public AsyncServiceScope GetAsyncScope<T>(ITestOutputHelper testOutputHelper)
```

### Accessing the keyed wired up services in .NET 8.0
### Accessing the keyed wired up services in .NET 8.0+

You can call the following method to access the keyed already-wired up services:

Expand Down
2 changes: 1 addition & 1 deletion azure-pipeline-PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pool:
steps:
- script: echo Build Number $(name)
- task: UseDotNet@2
displayName: 'Use .NET 8.0 sdk'
displayName: 'Use .NET 9.0 sdk'
inputs:
packageType: sdk
version: 9.0.100
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pool:
steps:
- script: echo Build Number $(name)
- task: UseDotNet@2
displayName: 'Use .NET 8.0 sdk'
displayName: 'Use .NET 9.0 sdk'
inputs:
packageType: sdk
version: 9.0.100
Expand Down
11 changes: 8 additions & 3 deletions src/Abstracts/TestBedFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper)
{
return _serviceProvider;
}
if(!_servicesAdded)
if (!_servicesAdded)
{
AddServices(_services, Configuration);
_services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper)));
Expand All @@ -54,8 +54,10 @@ public AsyncServiceScope GetAsyncScope(ITestOutputHelper testOutputHelper)
public T? GetService<T>(ITestOutputHelper testOutputHelper)
=> GetServiceProvider(testOutputHelper).GetService<T>();

#if NET8_0_OR_GREATER
public T? GetKeyedService<T>([DisallowNull] string key, ITestOutputHelper testOutputHelper)
=> GetServiceProvider(testOutputHelper).GetKeyedService<T>(key);
#endif

// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~AbstractDependencyInjectionFixture()
Expand Down Expand Up @@ -83,12 +85,15 @@ public async ValueTask DisposeAsync()
}

protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration);

protected abstract IEnumerable<TestAppSettings> GetTestAppSettings();

protected virtual ILoggingBuilder AddLoggingProvider(ILoggingBuilder loggingBuilder, ILoggerProvider loggerProvider)
=> loggingBuilder.AddProvider(loggerProvider);

protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder) { }
protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder)
{
}

private IConfigurationRoot? GetConfigurationRoot()
{
Expand Down Expand Up @@ -130,4 +135,4 @@ protected virtual void Dispose(bool disposing)
}

protected abstract ValueTask DisposeAsyncCore();
}
}
50 changes: 42 additions & 8 deletions src/Xunit.Microsoft.DependencyInjection.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>True</IsPackable>
<PackageId>Xunit.Microsoft.DependencyInjection</PackageId>
<Title>Microsoft Dependency Injection utility for Xunit</Title>
<Version>9.0</Version>
<Authors>Arash Sabet</Authors>
<Company>Umplify Technologies Inc.</Company>
<PackageLicenseFile>..\LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://umplify.github.io/xunit-dependency-injection/</PackageProjectUrl>
<PackageIconUrl>http://umplify.com/wp-content/uploads/2017/11/umplify_logo.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Description>This package contains the necessary classes and features to leverage Xunit's fixture class for Microsoft dependency inection framework.</Description>
<Copyright>2024 - All rights reserved, Umplify Technologies Inc.</Copyright>
<PackageTags>xunit dependency-injection microsoft-dependency-injection xunit-dependency-injection</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit.abstractions" Version="[2.0.3,)" />
<PackageReference Include="xunit.core" Version="[2.9.2,)" />
</ItemGroup>

<PropertyGroup Condition="$(TargetFramework) == 'net6.0'">
<MinMicrosoftExtensionsVersion>6</MinMicrosoftExtensionsVersion>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework) == 'net7.0'">
<MinMicrosoftExtensionsVersion>7</MinMicrosoftExtensionsVersion>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework) == 'net8.0'">
<MinMicrosoftExtensionsVersion>8</MinMicrosoftExtensionsVersion>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework) == 'net9.0'">
<MinMicrosoftExtensionsVersion>9</MinMicrosoftExtensionsVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="xunit.core" Version="2.9.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MinMicrosoftExtensionsVersion).*" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MinMicrosoftExtensionsVersion).*" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MinMicrosoftExtensionsVersion).*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MinMicrosoftExtensionsVersion).*" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MinMicrosoftExtensionsVersion).*" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Abstracts\" />
Expand Down
17 changes: 0 additions & 17 deletions src/package.nuspec

This file was deleted.