diff --git a/CHANGELOG.md b/CHANGELOG.md index 15669deba..e255a338e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/docs/site/docs/test-doubles/fake-webassemblyhostenvironment.md b/docs/site/docs/test-doubles/fake-webassemblyhostenvironment.md new file mode 100644 index 000000000..787e85ea8 --- /dev/null +++ b/docs/site/docs/test-doubles/fake-webassemblyhostenvironment.md @@ -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 `` component: + +```cshtml +@inject IWebAssemblyHostEnvironment HostEnvironment + +

+ Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World"). +

+

+ The base URL is: @HostEnvironment.BaseAddress +

+``` + +To verify that the `` component correctly says hello to the developers, do the following: + +```csharp +// Arrange +using var ctx = new TestContext(); +var hostEnvironment = ctx.Services.GetRequiredService(); + +// 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(); + +// Assert - inspects markup to verify the message +cut.Find("#message").MarkupMatches($"

Hello Developers.

"); +``` + +To verify that the `` component correctly uses the current `BaseAddress`, do the following: + +```csharp +// Arrange +using var ctx = new TestContext(); +var hostEnvironment = ctx.Services.GetRequiredService(); + +// Sets a new base address directly on the BaseAddress property. +hostEnvironment.BaseAddress = "myBaseUrl/"; + +// Act +var cut = ctx.RenderComponent(); + +// Assert - inspect markup to verify that the BaseAddress is used correctly. +cut.Find("#address").MarkupMatches($"

The base URL is: myBaseUrl/

"); +``` diff --git a/docs/site/docs/test-doubles/index.md b/docs/site/docs/test-doubles/index.md index 1ffa50368..88a1de589 100644 --- a/docs/site/docs/test-doubles/index.md +++ b/docs/site/docs/test-doubles/index.md @@ -16,4 +16,5 @@ The built-in test doubles are described on the following pages: - - - +- - \ No newline at end of file diff --git a/docs/site/docs/toc.md b/docs/site/docs/toc.md index 7206babca..088eb6bf0 100644 --- a/docs/site/docs/toc.md +++ b/docs/site/docs/toc.md @@ -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) diff --git a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs index 1672ab8b9..ae1bc53d0 100644 --- a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs +++ b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs @@ -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; @@ -40,6 +41,10 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl services.AddSingleton(s => s.GetRequiredService()); services.AddSingleton(); + // bUnits fake WebAssemblyHostEnvironment + services.AddSingleton(); + services.AddSingleton(s => s.GetRequiredService()); + // bUnit specific services services.AddSingleton(testContext); services.AddSingleton(); diff --git a/src/bunit.web/TestDoubles/WebAssemblyHostEnvironment/FakeWebAssemblyHostEnvironment.cs b/src/bunit.web/TestDoubles/WebAssemblyHostEnvironment/FakeWebAssemblyHostEnvironment.cs new file mode 100644 index 000000000..6cdad1698 --- /dev/null +++ b/src/bunit.web/TestDoubles/WebAssemblyHostEnvironment/FakeWebAssemblyHostEnvironment.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace Bunit.TestDoubles; + +/// +/// Represents a fake that makes the and settable. +/// +public class FakeWebAssemblyHostEnvironment : IWebAssemblyHostEnvironment +{ + /// + /// Gets or sets the name of the environment. Default is Production. + /// + public string Environment { get; set; } = "Production"; + + /// + /// Gets or sets the base address. Default is /. + /// + public string BaseAddress { get; set; } = "/"; + + /// + /// Sets the property to Development. + /// + public void SetEnvironmentToDevelopment() + { + Environment = "Development"; + } + + /// + /// Sets the property to Staging. + /// + public void SetEnvironmentToStaging() + { + Environment = "Staging"; + } + + /// + /// Sets the property to Production. + /// + public void SetEnvironmentToProduction() + { + Environment = "Production"; + } + +} diff --git a/src/bunit.web/bunit.web.csproj b/src/bunit.web/bunit.web.csproj index eb11df92e..4ab311d5c 100644 --- a/src/bunit.web/bunit.web.csproj +++ b/src/bunit.web/bunit.web.csproj @@ -1,4 +1,4 @@ - + netstandard2.1;net5.0;net6.0;net7.0 @@ -24,6 +24,7 @@ + @@ -31,6 +32,7 @@ + @@ -38,6 +40,7 @@ + @@ -45,6 +48,7 @@ + diff --git a/tests/bunit.testassets/SampleComponents/SimpleUsingWebAssemblyHostEnvironment.razor b/tests/bunit.testassets/SampleComponents/SimpleUsingWebAssemblyHostEnvironment.razor new file mode 100644 index 000000000..9a3831ec5 --- /dev/null +++ b/tests/bunit.testassets/SampleComponents/SimpleUsingWebAssemblyHostEnvironment.razor @@ -0,0 +1,3 @@ +@inject IWebAssemblyHostEnvironment HostEnvironment + +

Hello @(HostEnvironment.IsDevelopment() ? "Developers" : "World"). The base URL is: @HostEnvironment.BaseAddress

diff --git a/tests/bunit.testassets/_Imports.razor b/tests/bunit.testassets/_Imports.razor index f630f94aa..53e2952cd 100644 --- a/tests/bunit.testassets/_Imports.razor +++ b/tests/bunit.testassets/_Imports.razor @@ -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 diff --git a/tests/bunit.testassets/bunit.testassets.csproj b/tests/bunit.testassets/bunit.testassets.csproj index dcfc3b9cb..432942423 100644 --- a/tests/bunit.testassets/bunit.testassets.csproj +++ b/tests/bunit.testassets/bunit.testassets.csproj @@ -22,19 +22,23 @@ + + + + diff --git a/tests/bunit.web.tests/TestDoubles/FakeWebAssemblyHostEnvironmentTest.cs b/tests/bunit.web.tests/TestDoubles/FakeWebAssemblyHostEnvironmentTest.cs new file mode 100644 index 000000000..b246aa38f --- /dev/null +++ b/tests/bunit.web.tests/TestDoubles/FakeWebAssemblyHostEnvironmentTest.cs @@ -0,0 +1,27 @@ +namespace Bunit.TestDoubles; + +public class FakeWebAssemblyHostEnvironmentTest : TestContext +{ + [Fact] + public void ShouldSayHelloToDevelopers() + { + var hostEnvironment = Services.GetRequiredService(); + hostEnvironment.SetEnvironmentToDevelopment(); + + var cut = RenderComponent(); + + // Assert - inspects markup to verify the message + cut.Find("p").MarkupMatches($"

Hello Developers. The base URL is: /

"); + } + + [Fact] + public void ShouldUseCorrectBaseAddress() + { + var hostEnvironment = Services.GetRequiredService(); + hostEnvironment.BaseAddress = "myBaseUrl/"; + var cut = RenderComponent(); + + // Assert - inspect markup to verify that the BaseAddress is used correctly. + cut.Find("p").MarkupMatches($"

Hello World. The base URL is: myBaseUrl/

"); + } +}