Skip to content

Commit 347a359

Browse files
committed
COnverted TestBed class into a non-abstract one to make overriding Clear() and DisposeAsync() optional #107
1 parent 520d3f4 commit 347a359

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed
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)