-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TUnit sample to the docs * Package updates --------- Co-authored-by: JT <Hawxy@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
1,437 additions
and
520 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
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
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,53 @@ | ||
# Integrating with TUnit | ||
|
||
Like other testing frameworks, you'll want to reuse the `IAlbaHost` across tests and test fixtures because | ||
`AlbaHost` is relatively expensive to create. To do that with TUnit, you should start by writing a bootstrapping class | ||
that inherits from `IAsyncInitializer` and `IAsyncDisposable`: | ||
|
||
<!-- snippet: sample_TUnit_Application --> | ||
<a id='snippet-sample_tunit_application'></a> | ||
```cs | ||
public sealed class AlbaBootstrap : IAsyncInitializer, IAsyncDisposable | ||
{ | ||
public IAlbaHost Host { get; private set; } = null!; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
Host = await AlbaHost.For<WebApp.Program>(); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await Host.DisposeAsync(); | ||
} | ||
} | ||
``` | ||
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/TUnitSamples/Program.cs#L6-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_tunit_application' title='Start of snippet'>anchor</a></sup> | ||
<!-- endSnippet --> | ||
|
||
Then inject the instance by added `[ClassDataSource<AlbaBootstrap>(Shared = SharedType.Globally)]` to your test class. We recommend creating a base class to allow easier access of the host and any other dependencies. | ||
|
||
<!-- snippet: sample_TUnit_scenario_test --> | ||
<a id='snippet-sample_tunit_scenario_test'></a> | ||
```cs | ||
public abstract class AlbaTestBase(AlbaBootstrap albaBootstrap) | ||
{ | ||
protected IAlbaHost Host => albaBootstrap.Host; | ||
} | ||
|
||
[ClassDataSource<AlbaBootstrap>(Shared = SharedType.Globally)] | ||
public class MyTestClass(AlbaBootstrap albaBootstrap) : AlbaTestBase(albaBootstrap) | ||
{ | ||
[Test] | ||
public async Task happy_path() | ||
{ | ||
await Host.Scenario(_ => | ||
{ | ||
_.Get.Url("/fake/okay"); | ||
_.StatusCodeShouldBeOk(); | ||
}); | ||
} | ||
} | ||
``` | ||
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/TUnitSamples/Program.cs#L23-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_tunit_scenario_test' title='Start of snippet'>anchor</a></sup> | ||
<!-- endSnippet --> |
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
Oops, something went wrong.