Skip to content

Commit bed9bd8

Browse files
committed
docs: Show new LifeCycle attribute
1 parent 8761655 commit bed9bd8

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NUnit.Framework;
2+
3+
namespace Bunit.Docs.Samples;
4+
5+
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
6+
public class HelloWorldInstancePerTestCase : Bunit.TestContext
7+
{
8+
[Test]
9+
public void HelloWorldComponentRendersCorrectly()
10+
{
11+
// Act
12+
var cut = RenderComponent<HelloWorld>();
13+
14+
// Assert
15+
cut.MarkupMatches("<h1>Hello world from Blazor</h1>");
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@attribute [FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
2+
@inherits Bunit.TestContext
3+
4+
@code {
5+
[Test]
6+
public void HelloWorldComponentRendersCorrectly()
7+
{
8+
// Act
9+
var cut = Render(@<HelloWorld />);
10+
11+
// Assert
12+
cut.MarkupMatches(@<h1>Hello world from Blazor</h1>);
13+
}
14+
}

docs/samples/tests/nunit/bunit.docs.nunit.samples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="nunit" Version="3.12.0" />
8+
<PackageReference Include="nunit" Version="3.13.3" />
99
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
1010
<PackageReference Include="Moq" Version="4.16.1" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ The test above does the following:
6868
2. Renders the `<HelloWorld>` component using <xref:Bunit.TestContext>, which is done through the `Render(RenderFragment)` method. We cover passing parameters to components on the <xref:passing-parameters-to-components> page.
6969
3. Verifies the rendered markup from the `<HelloWorld>` component using the `MarkupMatches` method. The `MarkupMatches` method performs a semantic comparison of the expected markup with the rendered markup.
7070

71+
Alternatively, use the [LifeCycle.InstancePerTestCase](https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html) attribute (introduced in NUnit 3.13) so that a new instance of the test class is created for each test removing the need for the wrapper.
72+
73+
[!code-csharp[HelloWorldInstancePerTestCase.cs](../../../samples/tests/nunit/HelloWorldInstancePerTestCase.cs#L5-L17)]
74+
7175
# [MSTest](#tab/mstest)
7276

7377
[!code-cshtml[HelloWorldRazorTest.razor](../../../samples/tests/mstest/HelloWorldRazorTest.razor)]
@@ -150,6 +154,10 @@ The test above does the following:
150154
2. Renders the `<HelloWorld>` component using <xref:Bunit.TestContext>, which is done through the <xref:Bunit.TestContext.RenderComponent``1(Action{Bunit.ComponentParameterCollectionBuilder{``0}})> method. We cover passing parameters to components on the <xref:passing-parameters-to-components> page.
151155
3. Verifies the rendered markup from the `<HelloWorld>` component using the `MarkupMatches` method. The `MarkupMatches` method performs a semantic comparison of the expected markup with the rendered markup.
152156

157+
Alternatively, use the [LifeCycle.InstancePerTestCase](https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html) attribute (introduced in NUnit 3.13) so that a new instance of the test class is created for each test removing the need for the wrapper.
158+
159+
[!code-csharp[HelloWorldRazorInstancePerTestCase.razor](../../../samples/tests/nunit/HelloWorldInstancePerTestCase.cs)]
160+
153161
# [MSTest](#tab/mstest)
154162

155163
[!code-csharp[HelloWorldTest.cs](../../../samples/tests/mstest/HelloWorldTest.cs#L3-L20)]

0 commit comments

Comments
 (0)