-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dbfb71
commit 212c90d
Showing
3 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using Alba; | ||
using Marten; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Oakton; | ||
using Wolverine.Tracking; | ||
using Xunit; | ||
|
||
namespace Helpdesk.Api.Tests; | ||
|
||
public class AppFixture : IAsyncLifetime | ||
{ | ||
public IAlbaHost Host { get; private set; } | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
// Sorry folks, but this is absolutely necessary if you | ||
// use Oakton for command line processing and want to | ||
// use WebApplicationFactory and/or Alba for integration testing | ||
OaktonEnvironment.AutoStartHost = true; | ||
|
||
// This is bootstrapping the actual application using | ||
// its implied Program.Main() set up | ||
Host = await AlbaHost.For<Program>(x => { }); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
if (Host != null) | ||
{ | ||
return Host.DisposeAsync().AsTask(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task bootstrap(int delay) | ||
{ | ||
if (Host != null) | ||
{ | ||
try | ||
{ | ||
var endpoints = Host.Services.GetRequiredService<EndpointDataSource>().Endpoints; | ||
if (endpoints.Count < 5) | ||
{ | ||
throw new Exception($"Only got {endpoints.Count} endpoints, something is missing!"); | ||
} | ||
|
||
await Host.GetAsText("/trace"); | ||
await Task.Delay(delay); | ||
return; | ||
} | ||
catch (Exception e) | ||
{ | ||
await Host.StopAsync(); | ||
Host = null; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
[CollectionDefinition("integration")] | ||
public class IntegrationCollection : ICollectionFixture<AppFixture> | ||
{ | ||
} | ||
|
||
[Collection("integration")] | ||
public abstract class IntegrationContext : IAsyncLifetime | ||
{ | ||
private readonly AppFixture _fixture; | ||
|
||
protected IntegrationContext(AppFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
// more.... | ||
|
||
public IAlbaHost Host => _fixture.Host; | ||
|
||
public IDocumentStore Store => _fixture.Host.Services.GetRequiredService<IDocumentStore>(); | ||
|
||
async Task IAsyncLifetime.InitializeAsync() | ||
{ | ||
// Using Marten, wipe out all data and reset the state | ||
// back to exactly what we described in InitialAccountData | ||
await Store.Advanced.ResetAllData(); | ||
} | ||
|
||
// This is required because of the IAsyncLifetime | ||
// interface. Note that I do *not* tear down database | ||
// state after the test. That's purposeful | ||
public Task DisposeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
|
||
public async Task<IScenarioResult> Scenario(Action<Scenario> configure) | ||
{ | ||
return await Host.Scenario(configure); | ||
} | ||
|
||
// This method allows us to make HTTP calls into our system | ||
// in memory with Alba, but do so within Wolverine's test support | ||
// for message tracking to both record outgoing messages and to ensure | ||
// that any cascaded work spawned by the initial command is completed | ||
// before passing control back to the calling test | ||
protected async Task<(ITrackedSession, IScenarioResult)> TrackedHttpCall(Action<Scenario> configuration) | ||
{ | ||
IScenarioResult result = null; | ||
|
||
// The outer part is tying into Wolverine's test support | ||
// to "wait" for all detected message activity to complete | ||
var tracked = await Host.ExecuteAndWaitAsync(async () => | ||
{ | ||
// The inner part here is actually making an HTTP request | ||
// to the system under test with Alba | ||
result = await Host.Scenario(configuration); | ||
}); | ||
|
||
return (tracked, result); | ||
} | ||
} |
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,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly:InternalsVisibleTo("Helpdesk.Api.Tests")] |