|
| 1 | +--- |
| 2 | +uid: fake-webassemblyhostenvironment |
| 3 | +title: Faking IWebAssemblyHostEnvironment |
| 4 | +--- |
| 5 | + |
| 6 | +# Faking `IWebAssemblyHostEnvironment` |
| 7 | + |
| 8 | +bUnit has a fake implementation of Blazor's `IWebAssemblyHostEnvironment` built-in, which is added by default to bUnit's `TestContext.Services` service provider. That means nothing special is needed to test components that depend on `IWebAssemblyHostEnvironment`, as it is already available by default. |
| 9 | + |
| 10 | +Out of the box, the fake implementation has its `Environment` property set to `production`, and its `BaseAddress` set to `/`. |
| 11 | + |
| 12 | +## Setting `Environment` and `BaseAddress` |
| 13 | + |
| 14 | +Lets look at a few examples of how to set the two `IWebAssemblyHostEnvironment` properties `Environment` and `BaseAddress` via the built-in fake. |
| 15 | + |
| 16 | +In the examples, we'll use the following `<HelloWorld>` component: |
| 17 | + |
| 18 | +```cshtml |
| 19 | +@inject IWebAssemblyHostEnvironment HostEnvironment |
| 20 | +
|
| 21 | +<p id="message"> |
| 22 | + Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World"). |
| 23 | +</p> |
| 24 | +<p id="address"> |
| 25 | + The base URL is: @HostEnvironment.BaseAddress |
| 26 | +</p> |
| 27 | +``` |
| 28 | + |
| 29 | +To verify that the `<HelloWorld>` component correctly says hello to the developers, do the following: |
| 30 | + |
| 31 | +```csharp |
| 32 | +// Arrange |
| 33 | +using var ctx = new TestContext(); |
| 34 | +var hostEnvironment = ctx.Services.GetRequiredService<FakeWebAssemblyHostEnvironment>(); |
| 35 | + |
| 36 | +// Sets the environment to "Development". There are two other helper |
| 37 | +// methods available as well, SetEnvironmentToProduction() and |
| 38 | +// set SetEnvironmentToStaging(), or environment can also be changed |
| 39 | +// directly through the hostEnvironment.Environment property. |
| 40 | +hostEnvironment.SetEnvironmentToDevelopment(); |
| 41 | + |
| 42 | +var cut = ctx.RenderComponent<SimpleUsingWebAssemblyHostEnvironment>(); |
| 43 | + |
| 44 | +// Assert - inspects markup to verify the message |
| 45 | +cut.Find("#message").MarkupMatches($"<p>Hello Developers.</p>"); |
| 46 | +``` |
| 47 | + |
| 48 | +To verify that the `<HelloWorld>` component correctly uses the current `BaseAddress`, do the following: |
| 49 | + |
| 50 | +```csharp |
| 51 | +// Arrange |
| 52 | +using var ctx = new TestContext(); |
| 53 | +var hostEnvironment = ctx.Services.GetRequiredService<FakeWebAssemblyHostEnvironment>(); |
| 54 | + |
| 55 | +// Sets a new base address directly on the BaseAddress property. |
| 56 | +hostEnvironment.BaseAddress = "myBaseUrl/"; |
| 57 | + |
| 58 | +// Act |
| 59 | +var cut = ctx.RenderComponent<SimpleUsingWebAssemblyHostEnvironment>(); |
| 60 | + |
| 61 | +// Assert - inspect markup to verify that the BaseAddress is used correctly. |
| 62 | +cut.Find("#address").MarkupMatches($"<p>The base URL is: myBaseUrl/</p>"); |
| 63 | +``` |
0 commit comments