Skip to content

Commit 50fa3bb

Browse files
authored
Merge pull request #108 from Umplify/107-making-both-clear-and-disposeasynccore-virtual
Making both Clear and DisposeAsyncCore methods virtual
2 parents 520d3f4 + eac8bff commit 50fa3bb

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ Also, the test class should be decorated by the following attribute:
4848
[CollectionDefinition("Dependency Injection")]
4949
```
5050

51+
#### Clearing managed resources
52+
53+
To have managed resources cleaned up, simply override the virtual method of `Clear()`. This is an optional step.
54+
55+
#### Clearing managed resourced asynchronously
56+
57+
Simply override the virtual method of `DisposeAsyncCore()` for this purpose. This is also an optional step.
58+
5159
## Running tests in order
60+
5261
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.
5362

5463
Decorate your Xunit test class with the following attribute and associate ```TestOrder(...)``` with ```Fact``` and ```Theory```:

azure-pipeline-PR.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ steps:
1717
displayName: 'Use .NET 6.0 sdk'
1818
inputs:
1919
packageType: sdk
20-
version: 6.0.200
20+
version: 6.0.201
2121
installationPath: $(Agent.ToolsDirectory)/dotnet
2222
- script: echo Started restoring the source code
2323
- task: DotNetCoreCLI@2

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ steps:
1919
displayName: 'Use .NET 6.0 sdk'
2020
inputs:
2121
packageType: sdk
22-
version: 6.0.200
22+
version: 6.0.201
2323
installationPath: $(Agent.ToolsDirectory)/dotnet
2424
- script: echo Started restoring the source code
2525
- task: DotNetCoreCLI@2
Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
1-
using Microsoft.Extensions.Options;
1+
using Microsoft.Extensions.Options;
22
using Options = Xunit.Microsoft.DependencyInjection.ExampleTests.Services.Options;
33

4-
namespace Xunit.Microsoft.DependencyInjection.ExampleTests
4+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
5+
6+
public class IntegrationTests : TestBed<TestFixture>
57
{
6-
public class IntegrationTests : TestBed<TestFixture>
8+
public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
9+
: base(testOutputHelper, fixture)
710
{
8-
public IntegrationTests(ITestOutputHelper testOutputHelper, TestFixture fixture)
9-
: base(testOutputHelper, fixture)
10-
{
11-
}
12-
13-
[Theory]
14-
[InlineData(1, 2)]
15-
public void Test1(int x, int y)
16-
{
17-
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper);
18-
var option = _fixture.GetService<IOptions<Options>>(_testOutputHelper);
19-
var calculated = calculator?.Add(x, y);
20-
var expected = option?.Value.Rate * (x + y);
21-
Assert.True(expected == calculated);
22-
}
23-
24-
[Theory]
25-
[InlineData(1, 2)]
26-
public void Test2(int x, int y)
27-
{
28-
var calculator = _fixture.GetScopedService<ICalculator>(_testOutputHelper);
29-
var option = _fixture.GetScopedService<IOptions<Options>>(_testOutputHelper);
30-
var calculated = calculator?.Add(x, y);
31-
var expected = option?.Value.Rate * (x + y);
32-
Assert.True(expected == calculated);
33-
}
11+
}
3412

35-
protected override void Clear()
36-
{
37-
}
13+
[Theory]
14+
[InlineData(1, 2)]
15+
public void Test1(int x, int y)
16+
{
17+
var calculator = _fixture.GetService<ICalculator>(_testOutputHelper);
18+
var option = _fixture.GetService<IOptions<Options>>(_testOutputHelper);
19+
var calculated = calculator?.Add(x, y);
20+
var expected = option?.Value.Rate * (x + y);
21+
Assert.True(expected == calculated);
22+
}
3823

39-
protected override ValueTask DisposeAsyncCore()
40-
=> new();
24+
[Theory]
25+
[InlineData(1, 2)]
26+
public void Test2(int x, int y)
27+
{
28+
var calculator = _fixture.GetScopedService<ICalculator>(_testOutputHelper);
29+
var option = _fixture.GetScopedService<IOptions<Options>>(_testOutputHelper);
30+
var calculated = calculator?.Add(x, y);
31+
var expected = option?.Value.Rate * (x + y);
32+
Assert.True(expected == calculated);
4133
}
4234
}

src/Abstracts/TestBed.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Xunit.Microsoft.DependencyInjection.Abstracts;
22

3-
public abstract class TestBed<TFixture> : IDisposable, IClassFixture<TFixture>, IAsyncDisposable
3+
public class TestBed<TFixture> : IDisposable, IClassFixture<TFixture>, IAsyncDisposable
44
where TFixture : class
55
{
66
protected readonly ITestOutputHelper _testOutputHelper;
@@ -41,8 +41,6 @@ public void Dispose()
4141
GC.SuppressFinalize(this);
4242
}
4343

44-
protected abstract void Clear();
45-
4644
public async ValueTask DisposeAsync()
4745
{
4846
if (!_disposedAsync)
@@ -53,5 +51,6 @@ public async ValueTask DisposeAsync()
5351
}
5452
}
5553

56-
protected abstract ValueTask DisposeAsyncCore();
54+
protected virtual void Clear() { }
55+
protected virtual ValueTask DisposeAsyncCore() => new();
5756
}

0 commit comments

Comments
 (0)