-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for TUnit Class Constructors (#1362)
- Loading branch information
1 parent
fd58c7b
commit ad73307
Showing
9 changed files
with
59 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Verify.TUnit.Tests/ClassConstructor/ClassConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TUnit.Core.Interfaces; | ||
|
||
public class ClassConstructor : IClassConstructor | ||
{ | ||
static readonly IServiceProvider serviceProvider = CreateServiceProvider(); | ||
|
||
AsyncServiceScope scope; | ||
|
||
public T Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(ClassConstructorMetadata classConstructorMetadata) | ||
where T : class | ||
{ | ||
scope = serviceProvider.CreateAsyncScope(); | ||
return ActivatorUtilities.GetServiceOrCreateInstance<T>(scope.ServiceProvider); | ||
} | ||
|
||
static IServiceProvider CreateServiceProvider() => | ||
new ServiceCollection() | ||
.AddSingleton<string>("SingletonValue") | ||
.BuildServiceProvider(); | ||
} |
1 change: 1 addition & 0 deletions
1
...ify.TUnit.Tests/ClassConstructor/ClassConstructorMixedWithArgumentTests.Test.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SingletonValue |
1 change: 1 addition & 0 deletions
1
...Tests/ClassConstructor/ClassConstructorMixedWithArgumentTests.Test_service=2.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
11 changes: 11 additions & 0 deletions
11
src/Verify.TUnit.Tests/ClassConstructor/ClassConstructorMixedWithArgumentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[ClassConstructor<ClassConstructor>] | ||
[Arguments("2")] | ||
public class ClassConstructorMixedWithArgumentTests(string service) | ||
{ | ||
[Test] | ||
public async Task Test() | ||
{ | ||
await Assert.That(service).IsNotNull(); | ||
await Verify(service); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Verify.TUnit.Tests/ClassConstructor/ClassConstructorTests.Test.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SingletonValue |
10 changes: 10 additions & 0 deletions
10
src/Verify.TUnit.Tests/ClassConstructor/ClassConstructorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ClassConstructor<ClassConstructor>] | ||
public class ClassConstructorTests(string service) | ||
{ | ||
[Test] | ||
public async Task Test() | ||
{ | ||
await Assert.That(service).IsNotNull(); | ||
await Verify(service); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters