Skip to content

Commit

Permalink
Test integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Nov 13, 2023
1 parent 4dbfb71 commit 212c90d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions Helpdesk.Api.Tests/Helpdesk.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alba" Version="7.4.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
Expand Down
127 changes: 127 additions & 0 deletions Helpdesk.Api.Tests/IntegrationContext.cs
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);
}
}
3 changes: 3 additions & 0 deletions Helpdesk.Api/AssemblyAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Helpdesk.Api.Tests")]

0 comments on commit 212c90d

Please sign in to comment.