-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
415183a
commit 2e3ed60
Showing
11 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
docs/site/docs/test-doubles/fake-webassemblyhostenvironment.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/bunit.web/TestDoubles/WebAssemblyHostEnvironment/FakeWebAssemblyHostEnvironment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/bunit.testassets/SampleComponents/SimpleUsingWebAssemblyHostEnvironment.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/bunit.web.tests/TestDoubles/FakeWebAssemblyHostEnvironmentTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
} | ||
} |