Skip to content

Commit

Permalink
can use Alba web application factory + oakton
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy D. Miller authored and Jeremy D. Miller committed Nov 29, 2021
1 parent a22b0c7 commit 518b372
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Alba.Testing/Acceptance/web_application_factory_usage.cs
@@ -1,5 +1,8 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
#if NET6_0_OR_GREATER
using Oakton;
#endif
using Shouldly;
using Xunit;

Expand Down Expand Up @@ -67,6 +70,25 @@ public async Task bootstrapping_with_WebApplicationFactory()
var text2 = await host2.GetAsText("/api/values");
text2.ShouldBe("value1, value2");
}

[Fact]
public async Task using_with_oakton_as_runner()
{
// This is required. Sad trombone.
OaktonEnvironment.AutoStartHost = true;
await using var host = await AlbaHost.For<MinimalApiWithOakton.Program>(x =>
{
x.ConfigureServices((context, services) =>
{
services.AddSingleton<IService, ServiceA>();
});
});

host.Services.GetRequiredService<IService>().ShouldBeOfType<ServiceA>();

var text = await host.GetAsText("/");
text.ShouldBe("Hello World!");
}
}
#endif
}
1 change: 1 addition & 0 deletions src/Alba.Testing/Alba.Testing.csproj
Expand Up @@ -16,6 +16,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<ProjectReference Include="..\WebApiNet6\WebApiNet6.csproj" />
<ProjectReference Include="..\MinimalApiWithOakton\MinimalApiWithOakton.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Alba.sln
Expand Up @@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnitSamples", "NUnitSample
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiNet6", "WebApiNet6\WebApiNet6.csproj", "{C2D01782-E9BC-4E61-A4B7-76FA48292386}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalApiWithOakton", "MinimalApiWithOakton\MinimalApiWithOakton.csproj", "{2BD68525-60F2-4253-BFA7-12476F374EE9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +58,10 @@ Global
{C2D01782-E9BC-4E61-A4B7-76FA48292386}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2D01782-E9BC-4E61-A4B7-76FA48292386}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2D01782-E9BC-4E61-A4B7-76FA48292386}.Release|Any CPU.Build.0 = Release|Any CPU
{2BD68525-60F2-4253-BFA7-12476F374EE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BD68525-60F2-4253-BFA7-12476F374EE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BD68525-60F2-4253-BFA7-12476F374EE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BD68525-60F2-4253-BFA7-12476F374EE9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 13 additions & 0 deletions src/MinimalApiWithOakton/MinimalApiWithOakton.csproj
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Oakton" Version="4.1.0" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/MinimalApiWithOakton/Program.cs
@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Oakton;

namespace MinimalApiWithOakton
{
public class Program
{
public static Task<int> Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
return app.RunOaktonCommands(args);
}
}
}

28 changes: 28 additions & 0 deletions src/MinimalApiWithOakton/Properties/launchSettings.json
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35694",
"sslPort": 44353
}
},
"profiles": {
"MinimalApiWithOakton": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7161;http://localhost:5161",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions src/MinimalApiWithOakton/appsettings.Development.json
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/MinimalApiWithOakton/appsettings.json
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 518b372

Please sign in to comment.