Skip to content

Commit

Permalink
added: FakeWebAssembleHostEnvironment
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KristofferStrube authored Sep 13, 2022
1 parent 415183a commit 2e3ed60
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to **bUnit** will be documented in this file. The project ad

### Added

- Added new test double `FakeWebAssemblyHostEnvironment` that implements `IWebAssemblyHostEnvironment`. By [@KristofferStrube](https://github.com/KristofferStrube).

- Added `Bind` method to parameter builder that makes it easier to emulate the `@bind-Value` syntax in C#-based tests.

When writing tests in razor files, the `@bind-` directive can be directly applied like this:
Expand Down
63 changes: 63 additions & 0 deletions docs/site/docs/test-doubles/fake-webassemblyhostenvironment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
uid: fake-webassemblyhostenvironment
title: Faking IWebAssemblyHostEnvironment
---

# Faking `IWebAssemblyHostEnvironment`

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.

Out of the box, the fake implementation has its `Environment` property set to `production`, and its `BaseAddress` set to `/`.

## Setting `Environment` and `BaseAddress`

Lets look at a few examples of how to set the two `IWebAssemblyHostEnvironment` properties `Environment` and `BaseAddress` via the built-in fake.

In the examples, we'll use the following `<HelloWorld>` component:

```cshtml
@inject IWebAssemblyHostEnvironment HostEnvironment
<p id="message">
Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World").
</p>
<p id="address">
The base URL is: @HostEnvironment.BaseAddress
</p>
```

To verify that the `<HelloWorld>` component correctly says hello to the developers, do the following:

```csharp
// Arrange
using var ctx = new TestContext();
var hostEnvironment = ctx.Services.GetRequiredService<FakeWebAssemblyHostEnvironment>();

// Sets the environment to "Development". There are two other helper
// methods available as well, SetEnvironmentToProduction() and
// set SetEnvironmentToStaging(), or environment can also be changed
// directly through the hostEnvironment.Environment property.
hostEnvironment.SetEnvironmentToDevelopment();

var cut = ctx.RenderComponent<SimpleUsingWebAssemblyHostEnvironment>();

// Assert - inspects markup to verify the message
cut.Find("#message").MarkupMatches($"<p>Hello Developers.</p>");
```

To verify that the `<HelloWorld>` component correctly uses the current `BaseAddress`, do the following:

```csharp
// Arrange
using var ctx = new TestContext();
var hostEnvironment = ctx.Services.GetRequiredService<FakeWebAssemblyHostEnvironment>();

// Sets a new base address directly on the BaseAddress property.
hostEnvironment.BaseAddress = "myBaseUrl/";

// Act
var cut = ctx.RenderComponent<SimpleUsingWebAssemblyHostEnvironment>();

// Assert - inspect markup to verify that the BaseAddress is used correctly.
cut.Find("#address").MarkupMatches($"<p>The base URL is: myBaseUrl/</p>");
```
1 change: 1 addition & 0 deletions docs/site/docs/test-doubles/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ The built-in test doubles are described on the following pages:
- <xref:mocking-httpclient>
- <xref:faking-persistentcomponentstate>
- <xref:fake-navigation-manager>
- <xref:fake-webassemblyhostenvironment>
- <xref:input-file>
1 change: 1 addition & 0 deletions docs/site/docs/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
## [Mocking HttpClient](xref:mocking-httpclient)
## [Faking PersistentComponentState](xref:faking-persistentcomponentstate)
## [Faking NavigationManager](xref:fake-navigation-manager)
## [Faking IWebAssemblyHostEnvironment](xref:fake-webassemblyhostenvironment)
## [Testing with InputFile component](xref:input-file)

# [Miscellaneous testing tips](xref:misc-test-tips)
Expand Down
5 changes: 5 additions & 0 deletions src/bunit.web/Extensions/TestServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Localization;

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

// bUnits fake WebAssemblyHostEnvironment
services.AddSingleton<FakeWebAssemblyHostEnvironment>();
services.AddSingleton<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());

// bUnit specific services
services.AddSingleton<TestContextBase>(testContext);
services.AddSingleton<WebTestRenderer>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

namespace Bunit.TestDoubles;

/// <summary>
/// Represents a fake <see cref="IWebAssemblyHostEnvironment"/> that makes the <see cref="Environment"/> and <see cref="BaseAddress"/> settable.
/// </summary>
public class FakeWebAssemblyHostEnvironment : IWebAssemblyHostEnvironment
{
/// <summary>
/// Gets or sets the name of the environment. Default is <c>Production</c>.
/// </summary>
public string Environment { get; set; } = "Production";

/// <summary>
/// Gets or sets the base address. Default is <c>/</c>.
/// </summary>
public string BaseAddress { get; set; } = "/";

/// <summary>
/// Sets the <see cref="Environment"/> property to <c>Development</c>.
/// </summary>
public void SetEnvironmentToDevelopment()
{
Environment = "Development";
}

/// <summary>
/// Sets the <see cref="Environment"/> property to <c>Staging</c>.
/// </summary>
public void SetEnvironmentToStaging()
{
Environment = "Staging";
}

/// <summary>
/// Sets the <see cref="Environment"/> property to <c>Production</c>.
/// </summary>
public void SetEnvironmentToProduction()
{
Environment = "Production";
}

}
6 changes: 5 additions & 1 deletion src/bunit.web/bunit.web.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet5Version)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet6Version)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet7Version)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@inject IWebAssemblyHostEnvironment HostEnvironment

<p>Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World"). The base URL is: @HostEnvironment.BaseAddress</p>
1 change: 1 addition & 0 deletions tests/bunit.testassets/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Hosting
@using Microsoft.Extensions.DependencyInjection
@using Microsoft.Extensions.Logging
@using Microsoft.JSInterop
Expand Down
4 changes: 4 additions & 0 deletions tests/bunit.testassets/bunit.testassets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet3Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1" />
<PackageReference Include="System.Text.Json" Version="6.0.5" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet5Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet5Version)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet6Version)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(DotNet7Version)" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(DotNet7Version)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Bunit.TestDoubles;

public class FakeWebAssemblyHostEnvironmentTest : TestContext
{
[Fact]
public void ShouldSayHelloToDevelopers()
{
var hostEnvironment = Services.GetRequiredService<FakeWebAssemblyHostEnvironment>();
hostEnvironment.SetEnvironmentToDevelopment();

var cut = RenderComponent<SimpleUsingWebAssemblyHostEnvironment>();

// Assert - inspects markup to verify the message
cut.Find("p").MarkupMatches($"<p>Hello Developers. The base URL is: /</p>");
}

[Fact]
public void ShouldUseCorrectBaseAddress()
{
var hostEnvironment = Services.GetRequiredService<FakeWebAssemblyHostEnvironment>();
hostEnvironment.BaseAddress = "myBaseUrl/";
var cut = RenderComponent<SimpleUsingWebAssemblyHostEnvironment>();

// Assert - inspect markup to verify that the BaseAddress is used correctly.
cut.Find("p").MarkupMatches($"<p>Hello World. The base URL is: myBaseUrl/</p>");
}
}

0 comments on commit 2e3ed60

Please sign in to comment.