-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
web_application_factory_usage.cs
94 lines (79 loc) · 3.05 KB
/
web_application_factory_usage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Oakton;
using Shouldly;
using Xunit;
namespace Alba.Testing.Acceptance
{
public class web_application_factory_usage
{
[Fact]
public async Task handle_blow_ups()
{
// WebApplicationFactory can resolve old and new style of Program.cs
// .NET 6 style - the global:: namespace prefix would not be required in a normal test project
await using var host = await AlbaHost.For<global::Program>(x =>
{
x.ConfigureServices((context, services) =>
{
//...
});
});
var ex = await Should.ThrowAsync<ScenarioAssertionException>(async () =>
{
await host.Scenario(x =>
{
x.Get.Url("/blowup");
});
});
ex.Message.ShouldContain("Expected status code 200, but was 500");
}
public interface IService { }
public class ServiceA : IService { }
[Fact]
public async Task bootstrapping_with_WebApplicationFactory()
{
// WebApplicationFactory can resolve old and new style of Program.cs
// .NET 6 style - the global:: namespace prefix would not be required in a normal test project
#region sample_bootstrapping_with_web_application_factory
await using var host = await AlbaHost.For<global::Program>(x =>
{
x.ConfigureServices((context, services) =>
{
services.AddSingleton<IService, ServiceA>();
});
});
#endregion
host.Services.GetRequiredService<IService>().ShouldBeOfType<ServiceA>();
var text = await host.GetAsText("/");
text.ShouldBe("Hello World!");
// Works with .NET 5 / Traditional entry point
await using var host2 = await AlbaHost.For<WebApp.Program>(x =>
{
x.ConfigureServices((context, services) =>
{
services.AddSingleton<IService, ServiceA>();
});
});
host2.Services.GetRequiredService<IService>().ShouldBeOfType<ServiceA>();
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!");
}
}
}