Skip to content

Commit 2e3ed60

Browse files
added: FakeWebAssembleHostEnvironment
* Added FakeWebAssembleHostEnvironment, tests, and docs. * Updated CHANGELOG * Fixed bracket on wrong line. * Removed unnecesary automatic format changes. * Updated Microsoft.AspNetCore.Components.WebAssembly version * Changes from PR Review * Changes from second PR Review
1 parent 415183a commit 2e3ed60

File tree

11 files changed

+156
-1
lines changed

11 files changed

+156
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to **bUnit** will be documented in this file. The project ad
88

99
### Added
1010

11+
- Added new test double `FakeWebAssemblyHostEnvironment` that implements `IWebAssemblyHostEnvironment`. By [@KristofferStrube](https://github.com/KristofferStrube).
12+
1113
- Added `Bind` method to parameter builder that makes it easier to emulate the `@bind-Value` syntax in C#-based tests.
1214

1315
When writing tests in razor files, the `@bind-` directive can be directly applied like this:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
```

docs/site/docs/test-doubles/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ The built-in test doubles are described on the following pages:
1616
- <xref:mocking-httpclient>
1717
- <xref:faking-persistentcomponentstate>
1818
- <xref:fake-navigation-manager>
19+
- <xref:fake-webassemblyhostenvironment>
1920
- <xref:input-file>

docs/site/docs/toc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
## [Mocking HttpClient](xref:mocking-httpclient)
2828
## [Faking PersistentComponentState](xref:faking-persistentcomponentstate)
2929
## [Faking NavigationManager](xref:fake-navigation-manager)
30+
## [Faking IWebAssemblyHostEnvironment](xref:fake-webassemblyhostenvironment)
3031
## [Testing with InputFile component](xref:input-file)
3132

3233
# [Miscellaneous testing tips](xref:misc-test-tips)

src/bunit.web/Extensions/TestServiceProviderExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Components.Authorization;
66
using Microsoft.AspNetCore.Components.Routing;
7+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
78
using Microsoft.Extensions.Localization;
89

910
namespace Bunit.Extensions;
@@ -40,6 +41,10 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
4041
services.AddSingleton<NavigationManager>(s => s.GetRequiredService<FakeNavigationManager>());
4142
services.AddSingleton<INavigationInterception, FakeNavigationInterception>();
4243

44+
// bUnits fake WebAssemblyHostEnvironment
45+
services.AddSingleton<FakeWebAssemblyHostEnvironment>();
46+
services.AddSingleton<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());
47+
4348
// bUnit specific services
4449
services.AddSingleton<TestContextBase>(testContext);
4550
services.AddSingleton<WebTestRenderer>();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2+
3+
namespace Bunit.TestDoubles;
4+
5+
/// <summary>
6+
/// Represents a fake <see cref="IWebAssemblyHostEnvironment"/> that makes the <see cref="Environment"/> and <see cref="BaseAddress"/> settable.
7+
/// </summary>
8+
public class FakeWebAssemblyHostEnvironment : IWebAssemblyHostEnvironment
9+
{
10+
/// <summary>
11+
/// Gets or sets the name of the environment. Default is <c>Production</c>.
12+
/// </summary>
13+
public string Environment { get; set; } = "Production";
14+
15+
/// <summary>
16+
/// Gets or sets the base address. Default is <c>/</c>.
17+
/// </summary>
18+
public string BaseAddress { get; set; } = "/";
19+
20+
/// <summary>
21+
/// Sets the <see cref="Environment"/> property to <c>Development</c>.
22+
/// </summary>
23+
public void SetEnvironmentToDevelopment()
24+
{
25+
Environment = "Development";
26+
}
27+
28+
/// <summary>
29+
/// Sets the <see cref="Environment"/> property to <c>Staging</c>.
30+
/// </summary>
31+
public void SetEnvironmentToStaging()
32+
{
33+
Environment = "Staging";
34+
}
35+
36+
/// <summary>
37+
/// Sets the <see cref="Environment"/> property to <c>Production</c>.
38+
/// </summary>
39+
public void SetEnvironmentToProduction()
40+
{
41+
Environment = "Production";
42+
}
43+
44+
}

src/bunit.web/bunit.web.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
@@ -24,27 +24,31 @@
2424
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet3Version)" />
2525
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet3Version)" />
2626
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet3Version)" />
27+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
2728
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1" />
2829
</ItemGroup>
2930

3031
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
3132
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet5Version)" />
3233
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet5Version)" />
3334
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet5Version)" />
35+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet5Version)" />
3436
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet5Version)" />
3537
</ItemGroup>
3638

3739
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
3840
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet6Version)" />
3941
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet6Version)" />
4042
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet6Version)" />
43+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet6Version)" />
4144
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet6Version)" />
4245
</ItemGroup>
4346

4447
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
4548
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet7Version)" />
4649
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet7Version)" />
4750
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet7Version)" />
51+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet7Version)" />
4852
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet7Version)" />
4953
</ItemGroup>
5054

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@inject IWebAssemblyHostEnvironment HostEnvironment
2+
3+
<p>Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World"). The base URL is: @HostEnvironment.BaseAddress</p>

tests/bunit.testassets/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@using Microsoft.AspNetCore.Components.Forms
33
@using Microsoft.AspNetCore.Components.Routing
44
@using Microsoft.AspNetCore.Components.Web
5+
@using Microsoft.AspNetCore.Components.WebAssembly.Hosting
56
@using Microsoft.Extensions.DependencyInjection
67
@using Microsoft.Extensions.Logging
78
@using Microsoft.JSInterop

tests/bunit.testassets/bunit.testassets.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222

2323
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
2424
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet3Version)" />
25+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
2526
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1" />
2627
<PackageReference Include="System.Text.Json" Version="6.0.5" />
2728
</ItemGroup>
2829
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
2930
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet5Version)" />
31+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet5Version)" />
3032
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet5Version)" />
3133
</ItemGroup>
3234
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
3335
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet6Version)" />
36+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet6Version)" />
3437
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet6Version)" />
3538
</ItemGroup>
3639
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
3740
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet7Version)" />
41+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet7Version)" />
3842
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet7Version)" />
3943
</ItemGroup>
4044

0 commit comments

Comments
 (0)