Skip to content

Commit 28bcec5

Browse files
committed
Initial rewrite of docs. Basic intro is done.
1 parent 007a767 commit 28bcec5

29 files changed

Lines changed: 603 additions & 121 deletions

docs/.editorconfig

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
tab_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = false
9+
10+
[*.cs]
11+
indent_style = space
12+
tab_size = 2
13+
trim_trailing_whitespace = true
14+
insert_final_newline = false
15+
16+
[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}]
17+
tab_size = 2
18+
19+
[*.{htm,html,css,scss}]
20+
tab_size = 2
21+
22+
[*.json]
23+
tab_size = 2
24+
25+
[*.{ps1,psm1}]
26+
tab_size = 4
27+
28+
[*.sh]
29+
tab_size = 4
30+
end_of_line = lf
31+
32+
[*.{yml,yaml,md}]
33+
indent_style = space
34+
tab_size = 2
35+
36+
[*.md]
37+
trim_trailing_whitespace = false
38+
39+
[*.{cs,razor}]
40+
dotnet_diagnostic.BL0001.severity = none
41+
dotnet_diagnostic.BL0002.severity = none
42+
dotnet_diagnostic.BL0003.severity = none
43+
dotnet_diagnostic.BL0004.severity = none
44+
dotnet_diagnostic.BL0005.severity = none
45+
dotnet_diagnostic.BL0006.severity = none

docs/docfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"_description": "bUnit is a unit testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJsRuntime, and perform snapshot testing.",
7171
"_enableSearch": true,
7272
"_appLogoPath": "/images/blazor-logo.png",
73-
"_disableBreadcrumb": true
73+
"_disableBreadcrumb": true,
74+
"_disableFooter": true
7475
},
7576
"postProcessors": [],
7677
"markdownEngineName": "markdig",

docs/docs.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "docs", "docs.csproj", "{B6EC06B4-D7EA-46C0-BF20-DFC59A213250}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B6EC06B4-D7EA-46C0-BF20-DFC59A213250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B6EC06B4-D7EA-46C0-BF20-DFC59A213250}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B6EC06B4-D7EA-46C0-BF20-DFC59A213250}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B6EC06B4-D7EA-46C0-BF20-DFC59A213250}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {94A09891-FA4C-48F3-85AF-D6E7728A03FC}
24+
EndGlobalSection
25+
EndGlobal

docs/docs/getting-started/create-test-project.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,6 @@ dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.c
225225

226226
## Further reading
227227

228-
- <xref:writing-first-csharp-test>
229-
- <xref:misc-test-tips>
228+
Now you are ready to write some times. To learn how, continue reading the <xref:writing-csharp-tests> and <xref:writing-razor-tests> pages.
229+
230+
For addition tips and tricks that will make writing tests easier, see the <xref:misc-test-tips> page.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
uid: fixture-options
3+
title: Fixture test options
4+
---
5+
6+
# `<Fixture>` options
7+
8+
TODO!

docs/docs/getting-started/getting-started.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,11 @@ title: Getting started with bUnit
77

88
To get started writing tests for your Blazor components, you first need to set up a test project. Then you can start writing your tests, using either C# or Razor syntax.
99

10-
The sections below takes you through each of the steps you need to get started:
11-
12-
## Creating a test project
13-
14-
First you need a test project to store your component tests inside. This is covered on the <xref:create-test-project> page.
15-
16-
## The basics of testing Blazor components
17-
18-
To test a component, you first have to render it with parameters, cascading values, and services passed into it. Then, you need access to the component's instance and the markup it has produced, so you can inspect and interact with both.
19-
20-
There are three different ways of doing this in bUnit:
21-
22-
1. [**C# based tests**](/docs/csharp-based-testing.html)
10+
1. **<xref:create-test-project>** covers setting up a bUnit test project.
11+
2. **<xref:writing-csharp-tests>** covers the basics of writing tests in C#.
2312
With C# based tests, you write all your testing logic in C# files, i.e. like regular unit tests.
24-
2. [**Razor based tests**](/docs/razor-based-testing.html)
13+
3. **<xref:writing-razor-tests>** covers the basics of writing tests in Razor and C# syntax.
2514
With Razor based tests, you write tests in `.razor` files, which allows you to declare, in Razor syntax, the component under test and other markup fragments you need. You still write your assertions via C# in the .razor file, inside `@code {...}` blocks.
26-
3. [**Snapshot tests**](/docs/snapshot-testing.html)
27-
Snapshot tests are written in `.razor` files. A test contains a definition of an input markup/component and the expected output markup. The library will then automatically perform an semantic HTML comparison. Very little C# is needed in this, usually only to configure services.
28-
29-
To learn more, read the [Basics of Blazor component testing](/docs/basics-of-blazor-component-testing.html) page.
3015

3116
## Examples
3217

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
uid: snapshot-options
3+
title: SnapshotTest options
4+
---
5+
6+
# `<SnapshotTest>` options
7+
8+
TODO!
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
uid: writing-csharp-tests
3+
title: Writing tests in C# for Blazor components
4+
---
5+
6+
# Writing tests in C# for Blazor components
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+
## Creating a basic test
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 `TestContext` and `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;`
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 `TestContext` and `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.:
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 the `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. We will cover passing parameters to components elsewhere.
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+
50+
> [!TIP]
51+
> In bUnit tests, we like to use the abbreviation `CUT`, short for "component under test", to indicate the component that is being tested. This is inspired by the common testing abbreviation `SUT`, short for "system under test".
52+
53+
## Remove boilerplate code from tests
54+
55+
We can remove some boilerplate code from each test by making the <xref:Bunit.TestContext> implicitly available to the test class, so we do not have to have `using var ctx = new Bunit.TestContext();` in every test. This can be done like this:
56+
57+
# [xUnit](#tab/xunit)
58+
59+
[!code-csharp[HelloWorldImplicitContextTest.cs](../../samples/tests/xunit/HelloWorldImplicitContextTest.cs)]
60+
61+
Since xUnit instantiates test classes for each execution of test methods inside them and disposes them after each test method has run, we simply inherit from <xref:Bunit.TestContext>, and methods like <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.ComponentParameter[])> can now be called directly from each test, as seen in the listing above.
62+
63+
# [NUnit](#tab/nunit)
64+
65+
[!code-csharp[HelloWorldImplicitContextTest.cs](../../samples/tests/nunit/HelloWorldImplicitContextTest.cs)]
66+
67+
[!code-csharp[BunitTestContext.cs](../../samples/tests/nunit/BunitTestContext.cs)]
68+
69+
Since NUnit instantiates the test class is once, we cannot simply inherit directly from <xref:Bunit.TestContext>, as we want a fresh instance of <xref:Bunit.TestContext> for each test. Instead, we create a helper class, `BunitTestContext`, which is listed above, and use that to hook into NUnit's `[SetUp]` and `[TearDown]` methods, which runs before and after each test.
70+
71+
Then methods like <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.ComponentParameter[])> can now be called directly from each test, as seen in the listing above.
72+
73+
# [MSTest](#tab/mstest)
74+
75+
[!code-csharp[HelloWorldImplicitContextTest.cs](../../samples/tests/mstest/HelloWorldImplicitContextTest.cs)]
76+
77+
[!code-csharp[BunitTestContext.cs](../../samples/tests/mstest/BunitTestContext.cs)]
78+
79+
Since MSTest instantiates the test class is once, we cannot simply inherit directly from <xref:Bunit.TestContext>, as we want a fresh instance of <xref:Bunit.TestContext> for each test. Instead, we create a helper class, `BunitTestContext`, which is listed above, and use that to hook into MSTest's `[TestInitialize]` and `[TestCleanup]` methods, which runs before and after each test.
80+
81+
Then methods like <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.ComponentParameter[])> can now be called directly from each test, as seen in the listing above.
82+
83+
***
84+
85+
## Further reading
86+
87+
With the basics out of the way, we will next look at how to pass parameters and inject services into our components under test, and after that, cover in more details the various ways we can verify the outcome of a rendering.
88+
89+
- <xref:passing-parameters-to-components>
90+
- <xref:inject-services-into-components>
91+
- <xref:verify-markup>
92+
- <xref:verify-component-state>
93+
- <xref:trigger-event-handlers>

docs/docs/getting-started/writing-first-csharp-test.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)