|
| 1 | +--- |
| 2 | +uid: writing-first-csharp-test |
| 3 | +title: Writing the first C# test |
| 4 | +--- |
| 5 | + |
| 6 | +# Writing the first C# test |
| 7 | + |
| 8 | +Testing Blazor components is a different from testing regular C# classes: Blazor components are *rendered*, they have the *Blazor component life-cycle*, during which we can *provide input* to them and where they *produce output*. |
| 9 | + |
| 10 | +**bUnit** enables you to render the component you want to test, pass in parameters to it, inject services into it, and access the rendered component instance and the markup it has produced. |
| 11 | + |
| 12 | +Rendering a component happens through bUnit's <xref:Bunit.TestContext>, and the result of the rendering, a <xref:Bunit.IRenderedComponent`1>, provides access to the component instance, and the markup produced by the component. |
| 13 | + |
| 14 | +## Rendering a component |
| 15 | + |
| 16 | +Lets see a simple example, where we test the following `HelloWorld` component: |
| 17 | + |
| 18 | +[!code-cshtml[HelloWorld.razor](../../samples/Components/HelloWorld.razor)] |
| 19 | + |
| 20 | +# [xUnit](#tab/xunit) |
| 21 | + |
| 22 | +[!code-csharp[HelloWorldTest.cs](../../samples/tests/xunit/HelloWorldTest.cs)] |
| 23 | + |
| 24 | +# [NUnit](#tab/nunit) |
| 25 | + |
| 26 | +[!code-csharp[HelloWorldTest.cs](../../samples/tests/nunit/HelloWorldTest.cs)] |
| 27 | + |
| 28 | +> [!NOTE] |
| 29 | +> `TestContext` is an ambiguous reference between `<xref:Bunit.TestContext>` and `NUnit.Framework.TestContext`, so you have to specify the `Bunit` namespace when referencing `<xref:Bunit.TestContext>` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's <xref:Bunit.TestContext> a different name during import, e.g.: `using BunitTestContext = Bunit.TestContext;` |
| 30 | +
|
| 31 | +# [MSTest](#tab/mstest) |
| 32 | + |
| 33 | +[!code-csharp[HelloWorldTest.cs](../../samples/tests/mstest/HelloWorldTest.cs)] |
| 34 | + |
| 35 | +> [!NOTE] |
| 36 | +> `TestContext` is an ambiguous reference between `<xref:Bunit.TestContext>` and `Microsoft.VisualStudio.TestTools.UnitTesting.TestContext`, so you have to specify the `Bunit` namespace when referencing `<xref:Bunit.TestContext>` to resolve the ambiguity for the compiler. Alternatively, you can give bUnit's <xref:Bunit.TestContext> a different name during import, e.g.: |
| 37 | +> `using BunitTestContext = Bunit.TestContext;` |
| 38 | +
|
| 39 | +*** |
| 40 | + |
| 41 | +In this test, we do the following: |
| 42 | + |
| 43 | +1. New up the disposable <xref:Bunit.TestContext>, and assign it using `using var` syntax, to avoid unnecessary indention. |
| 44 | +2. Render the `HelloWorld` component using <xref:Bunit.TestContext>, which we do through the <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.ComponentParameter[])> method. |
| 45 | +3. Verify the rendered markup from the `HelloWorld` component using the <xref:Bunit.MarkupMatchesAssertExtensions.MarkupMatches(Bunit.IRenderedFragment,System.String,System.String)> method, which performs a semantic comparison of the expected markup with the rendered markup. |
| 46 | + |
| 47 | +> [!TIP] |
| 48 | +> Learn more about how the semantic HTML/markup comparison in bUnit work, and how to customize it on the <xref:semantic-html-comparison> page. |
| 49 | +
|
0 commit comments