Skip to content

Commit 18e456e

Browse files
committed
docs: fix code references, added missing samples
1 parent 9e35f78 commit 18e456e

File tree

8 files changed

+87
-42
lines changed

8 files changed

+87
-42
lines changed

docs/samples/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ root = true
33
[*]
44
indent_style = space
55
tab_size = 2
6+
indent_size = 2
7+
indent_width = 2
68
charset = utf-8
79
trim_trailing_whitespace = true
810
insert_final_newline = false
@@ -13,6 +15,8 @@ trim_trailing_whitespace = false
1315
[*.{cs,razor}]
1416
tab_size = 2
1517
indent_style = space
18+
indent_size = 2
19+
indent_width = 2
1620
dotnet_diagnostic.BL0001.severity = none
1721
dotnet_diagnostic.BL0002.severity = none
1822
dotnet_diagnostic.BL0003.severity = none
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Bunit;
3+
4+
namespace Bunit.Docs.Samples;
5+
6+
[TestClass]
7+
public class HelloWorldExplicitContext
8+
{
9+
[TestMethod]
10+
public void HelloWorldComponentRendersCorrectly()
11+
{
12+
// Arrange
13+
using var ctx = new Bunit.TestContext();
14+
15+
// Act
16+
var cut = ctx.RenderComponent<HelloWorld>();
17+
18+
// Assert
19+
cut.MarkupMatches("<h1>Hello world from Blazor</h1>");
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Bunit;
2+
using NUnit.Framework;
3+
4+
namespace Bunit.Docs.Samples;
5+
6+
public class HelloWorldExplicitContext
7+
{
8+
[Test]
9+
public void HelloWorldComponentRendersCorrectly()
10+
{
11+
// Arrange
12+
using var ctx = new Bunit.TestContext();
13+
14+
// Act
15+
var cut = ctx.RenderComponent<HelloWorld>();
16+
17+
// Assert
18+
cut.MarkupMatches("<h1>Hello world from Blazor</h1>");
19+
}
20+
}

docs/samples/tests/xunit/HelloWorldExplicitContext.cs renamed to docs/samples/tests/xunit/HelloWorldExplicitContextTest.cs

File renamed without changes.

docs/site/docs/getting-started/writing-tests.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Rendering a component happens through bUnit's <xref:Bunit.TestContext>. The resu
1313

1414
## Write tests in `.cs` or `.razor` files
1515

16-
bUnit works with MSTest, NUnit, and xUnit, and it allows you to write the unit tests in either `.cs` or `.razor` files.
16+
bUnit works with MSTest, NUnit, and xUnit, and it allows you to write the unit tests in either `.cs` or `.razor` files.
1717

1818
The latter, writing tests in `.razor` files, provides an easier way to declare component markup and HTML markup in the tests, so it will most likely be the go-to for many people in the future.
1919

@@ -26,8 +26,8 @@ The following sections show how to get started writing tests in either `.cs` or
2626
Before writing tests in `.razor` files, a few things needs to be in place:
2727

2828
1. Make sure the test project has the SDK type set to `Microsoft.NET.Sdk.Razor`. Otherwise the Blazor compiler will not translate your `.razor` files into runnable code.
29-
2. Add an `_Imports.razor` file to the test project. It serves the same purpose as `_Imports.razor` files in regular Blazor projects. These using statements are useful to add right away:
30-
29+
2. Add an `_Imports.razor` file to the test project. It serves the same purpose as `_Imports.razor` files in regular Blazor projects. These using statements are useful to add right away:
30+
3131
```cshtml
3232
@using Microsoft.AspNetCore.Components.Forms
3333
@using Microsoft.AspNetCore.Components.Web
@@ -37,7 +37,7 @@ Before writing tests in `.razor` files, a few things needs to be in place:
3737
@using Bunit
3838
@using Bunit.TestDoubles
3939
```
40-
40+
4141
Also add an using statement for your general purpose testing framework, e.g. `@using Xunit` for xUnit.
4242

4343
With that in place, lets look at a simple example that tests the following `<HelloWorld>` component:
@@ -180,22 +180,22 @@ Just be aware that all examples in the rest of the documentation assumes that yo
180180

181181
# [xUnit](#tab/xunit)
182182

183-
[!code-csharp[HelloWorldExplicitContextTest.cs](../../../samples/tests/xunit/HelloWorldExplicitContextTest.cs#L3-L20)]
183+
[!code-csharp[HelloWorldExplicitContextTest.cs](../../../samples/tests/xunit/HelloWorldExplicitContextTest.cs#L6-L20)]
184184

185185
# [NUnit](#tab/nunit)
186186

187-
[!code-csharp[HelloWorldExplicitContextTest.cs](../../../samples/tests/nunit/HelloWorldExplicitContextTest.cs#L3-L20)]
187+
[!code-csharp[HelloWorldExplicitContextTest.cs](../../../samples/tests/nunit/HelloWorldExplicitContextTest.cs#L6-L20)]
188188

189189
> [!NOTE]
190-
> `TestContext` is an ambiguous reference - it could mean `Bunit.TestContext` or `NUnit.Framework.TestContext` - so you have to specify the `Bunit` namespace when referencing `TestContext` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's `TestContext` a different name during import, e.g.: `using BunitTestContext = Bunit.TestContext;`
190+
> `TestContext` is an ambiguous reference - it could mean `Bunit.TestContext` or `NUnit.Framework.TestContext` - so you have to specify the `Bunit` namespace when referencing `TestContext` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's `TestContext` a different name during import, e.g.: `using BunitTestContext = Bunit.TestContext;`
191191
192192
# [MSTest](#tab/mstest)
193193

194-
[!code-csharp[HelloWorldImplicitContextTest.cs](../../../samples/tests/mstest/HelloWorldExplicitContextTest.cs#L3-L20)]
194+
[!code-csharp[HelloWorldImplicitContextTest.cs](../../../samples/tests/mstest/HelloWorldExplicitContextTest.cs#L6-L21)]
195195

196196
> [!NOTE]
197-
> `TestContext` is an ambiguous reference - it could mean `Bunit.TestContext` or `Microsoft.VisualStudio.TestTools.UnitTesting.TestContext` - so you have to specify the `Bunit` namespace when referencing `TestContext` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's `TestContext` a different name during import, e.g.:
198-
> `using BunitTestContext = Bunit.TestContext;`
197+
> `TestContext` is an ambiguous reference - it could mean `Bunit.TestContext` or `Microsoft.VisualStudio.TestTools.UnitTesting.TestContext` - so you have to specify the `Bunit` namespace when referencing `TestContext` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's `TestContext` a different name during import, e.g.:
198+
> `using BunitTestContext = Bunit.TestContext;`
199199
200200
## Further reading
201201

docs/site/docs/interaction/trigger-renders.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ title: Triggering a render life cycle on a component
77

88
To trigger a re-render of a component under test, a reference to it through a <xref:Bunit.IRenderedComponent`1> type is needed. When using the <xref:Bunit.TestContext>'s `RenderComponent<TComponent>()` method, this is the type returned.
99

10-
In `.razor` based tests, using the <xref:Bunit.TestContext>'s <xref:Bunit.TestContext.Render``1(RenderFragment)> method also returns an <xref:Bunit.IRenderedComponent`1> (as opposed to the <xref:Bunit.TestContext.Render(RenderFragment)> method which returns the more simple <xref:Bunit.IRenderedFragment>).
10+
In `.razor` based tests, using the <xref:Bunit.TestContext>'s <xref:Bunit.TestContext.Render``1(RenderFragment)> method also returns an <xref:Bunit.IRenderedComponent`1> (as opposed to the <xref:Bunit.TestContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method which returns the more simple <xref:Bunit.IRenderedFragment>).
1111

1212
If you have a <xref:Bunit.IRenderedFragment> or a <xref:Bunit.IRenderedComponent`1> in a test, but need a child component's <xref:Bunit.IRenderedComponent`1>, then use the `FindComponent<TComponent>()` or the `FindComponents<TComponent>()` methods, which traverse down the render tree and finds rendered components.
1313

14-
With a <xref:Bunit.IRenderedComponent`1>, it is possible to cause the component to render again directly through the [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponentBase{``0})) method or one of the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.IRenderedFragmentBase.Bunit.RenderedFragmentInvokeAsyncExtensions.InvokeAsync(System.Action)) method.
14+
With a <xref:Bunit.IRenderedComponent`1>, it is possible to cause the component to render again directly through the [`Render()`](xref:Bunit.TestContext.Render``1(Microsoft.AspNetCore.Components.RenderFragment)) method or one of the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.IRenderedFragmentBase.Bunit.RenderedFragmentInvokeAsyncExtensions.InvokeAsync(System.Action)) method.
1515

1616
Let's look at how to use each of these methods to cause a re-render.
1717

@@ -21,7 +21,7 @@ The [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IR
2121

2222
[!code-csharp[](../../../samples/tests/xunit/ReRenderTest.cs?start=17&end=23&highlight=5)]
2323

24-
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponentBase{``0})).
24+
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponentBase{``0})).
2525

2626
> [!TIP]
2727
> The number of renders a component has been through can be inspected and verified using the <xref:Bunit.IRenderedFragmentBase.RenderCount> property.

0 commit comments

Comments
 (0)