Skip to content
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ Also, the test class should be decorated by the following attribute:
[CollectionDefinition("Dependency Injection")]
```

#### Clearing managed resources

To have managed resources cleaned up, simply override the virtual method of `Clear()`. This is an optional step.

#### Clearing managed resourced asynchronously

Simply override the virtual method of `DisposeAsyncCore()` for this purpose. This is also an optional step.

## Running tests in order

The library also has a bonus feature that simplifies running tests in order. The test class does not have to be derived from ```TestBed<T>``` class though and it can apply to all Xunit classes.

Decorate your Xunit test class with the following attribute and associate ```TestOrder(...)``` with ```Fact``` and ```Theory```:
Expand Down
2 changes: 1 addition & 1 deletion azure-pipeline-PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ steps:
displayName: 'Use .NET 6.0 sdk'
inputs:
packageType: sdk
version: 6.0.200
version: 6.0.201
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: echo Started restoring the source code
- task: DotNetCoreCLI@2
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ steps:
displayName: 'Use .NET 6.0 sdk'
inputs:
packageType: sdk
version: 6.0.200
version: 6.0.201
installationPath: $(Agent.ToolsDirectory)/dotnet
- script: echo Started restoring the source code
- task: DotNetCoreCLI@2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options;
using Options = Xunit.Microsoft.DependencyInjection.ExampleTests.Services.Options;

namespace Xunit.Microsoft.DependencyInjection.ExampleTests
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;

public class IntegrationTests : TestBed<TestFixture>
{
public class IntegrationTests : TestBed<TestFixture>
public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
: base(testOutputHelper, fixture)
{
public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
: base(testOutputHelper, fixture)
{
}

[Theory]
[InlineData(1, 2)]
public void Test1(int x, int y)
{
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper);
var option = _fixture.GetService<IOptions<Options>>(_testOutputHelper);
var calculated = calculator?.Add(x, y);
var expected = option?.Value.Rate * (x + y);
Assert.True(expected == calculated);
}

[Theory]
[InlineData(1, 2)]
public void Test2(int x, int y)
{
var calculator = _fixture.GetScopedService<ICalculator>(_testOutputHelper);
var option = _fixture.GetScopedService<IOptions<Options>>(_testOutputHelper);
var calculated = calculator?.Add(x, y);
var expected = option?.Value.Rate * (x + y);
Assert.True(expected == calculated);
}
}

protected override void Clear()
{
}
[Theory]
[InlineData(1, 2)]
public void Test1(int x, int y)
{
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper);
var option = _fixture.GetService<IOptions<Options>>(_testOutputHelper);
var calculated = calculator?.Add(x, y);
var expected = option?.Value.Rate * (x + y);
Assert.True(expected == calculated);
}

protected override ValueTask DisposeAsyncCore()
=> new();
[Theory]
[InlineData(1, 2)]
public void Test2(int x, int y)
{
var calculator = _fixture.GetScopedService<ICalculator>(_testOutputHelper);
var option = _fixture.GetScopedService<IOptions<Options>>(_testOutputHelper);
var calculated = calculator?.Add(x, y);
var expected = option?.Value.Rate * (x + y);
Assert.True(expected == calculated);
}
}
7 changes: 3 additions & 4 deletions src/Abstracts/TestBed.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Xunit.Microsoft.DependencyInjection.Abstracts;

public abstract class TestBed<TFixture> : IDisposable, IClassFixture<TFixture>, IAsyncDisposable
public class TestBed<TFixture> : IDisposable, IClassFixture<TFixture>, IAsyncDisposable
where TFixture : class
{
protected readonly ITestOutputHelper _testOutputHelper;
Expand Down Expand Up @@ -41,8 +41,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected abstract void Clear();

public async ValueTask DisposeAsync()
{
if (!_disposedAsync)
Expand All @@ -53,5 +51,6 @@ public async ValueTask DisposeAsync()
}
}

protected abstract ValueTask DisposeAsyncCore();
protected virtual void Clear() { }
protected virtual ValueTask DisposeAsyncCore() => new();
}