Skip to content

Commit 8c43990

Browse files
committed
updated docs
1 parent cf906cd commit 8c43990

File tree

12 files changed

+75
-118
lines changed

12 files changed

+75
-118
lines changed

docs/api/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# PLACEHOLDER
2-
TODO: Add .NET projects to the *src* folder and run `docfx` to generate **REAL** *API Documentation*!
1+
# bUnit References
2+
3+
TODO: write intro

docs/docfx.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
{
44
"src": [
55
{
6-
"files": [ "bunit.csproj" ],
6+
"files": [
7+
"bunit.core/bunit.core.csproj",
8+
"bunit.web/bunit.web.csproj",
9+
"bunit.xunit/bunit.xunit.csproj"
10+
],
711
"exclude": [ "**/bin/**", "**/obj/**" ],
812
"src": "../src"
913
}
@@ -65,7 +69,8 @@
6569
"_appTitle": "bUnit",
6670
"_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.",
6771
"_enableSearch": true,
68-
"_appLogoPath": "/images/blazor-logo.png"
72+
"_appLogoPath": "/images/blazor-logo.png",
73+
"_disableBreadcrumb": true
6974
},
7075
"postProcessors": [],
7176
"markdownEngineName": "markdig",

docs/docs/csharp-based-testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public class ComponentTest : TestContext // implements the ITestContext interfac
172172

173173
See the page [Mocking JsRuntime](/docs/mocking-jsruntime.html) for more details mock.
174174

175-
**Further reading:**
175+
## Further reading:
176176

177177
- [Semantic HTML markup comparison](/docs/semantic-html-markup-comparison.html)
178178
- [Mocking JsRuntime](/docs/mocking-jsruntime.html)
179-
- [C# test examples](/docs/csharp-test-examples.html)
179+
- [C# test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/Tests)

docs/docs/csharp-test-examples.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# C# test examples
22

3+
> **WARNING:** These examples are somewhat outdated. Some content in here might still apply:
4+
35
In the following examples, the terminology **component under test** (abbreviated CUT) is used to mean the component that is the target of the test. The examples below use the `Shouldly` assertion library as well. If you prefer not to use that just replace the assertions with the ones from your own favorite assertion library.
46

57
All examples can be found in the [Tests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/razor-components-testing-library/tree/master/sample/).

docs/docs/index.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ The sections below takes you through each of the steps you need to get started:
88

99
First you need a test project to store your component tests inside. Depending on what general purpose unit testing framework you prefer, you have a few options:
1010

11-
- [Creating a new bUnit and xUnit test project](creating-a-new-bunit-xunit-project.html)
12-
- [Creating a new bUnit and NUnit test project](creating-a-new-bunit-nunit-project.html)
13-
- [Creating a new bUnit and MSTest test project](creating-a-new-bunit-mstest-project.html)
11+
- [Creating a new bUnit and xUnit test project](/docs/creating-a-new-bunit-xunit-project.html)
12+
- [Creating a new bUnit and NUnit test project](/docs/creating-a-new-bunit-nunit-project.html)
13+
- [Creating a new bUnit and MSTest test project](/docs/creating-a-new-bunit-mstest-project.html)
1414

1515
## The basics of testing Blazor components
1616

1717
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.
1818

1919
There are three different ways of doing this in bUnit:
2020

21-
1. [**C# based tests**](csharp-based-testing.html)
21+
1. [**C# based tests**](/docs/csharp-based-testing.html)
2222
With C# based tests, you write all your testing logic in C# files, i.e. like regular unit tests.
23-
2. [**Razor based tests**](razor-based-testing.html)
23+
2. [**Razor based tests**](/docs/razor-based-testing.html)
2424
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.
25-
3. [**Snapshot tests**](snapshot-testing.html)
25+
3. [**Snapshot tests**](/docs/snapshot-testing.html)
2626
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.
2727

28-
To learn more, read the [Basics of Blazor component testing](basics-of-blazor-component-testing.html) page.
28+
To learn more, read the [Basics of Blazor component testing](/docs/basics-of-blazor-component-testing.html) page.
2929

3030
## Examples
3131

3232
To see examples of how to work assert and manipulate a rendered component, go to the following pages:
3333

34-
- [C# test examples](/docs/csharp-test-examples.html)
35-
- [Razor test examples](/docs/razor-test-examples.html)
34+
- [C# test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/Tests)
35+
- [Razor based test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/RazorTestComponents)
36+
- [Snapshot test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/SnapshotTests)
37+

docs/docs/razor-based-testing.md

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
# Razor-based testing
22

3-
This pages documents how to do Blazor/Razor component testing from `.razor` files.
4-
5-
Before you get started, make sure you have read the [Getting started](/docs/getting-started.html) page and in particular the [Basics of Blazor component testing](/docs/basics-of-blazor-component-testing.html) section. It wont take long, and it will ensure you get a good start at component testing.
6-
7-
> **NOTE:** This feature is _EXPERIMENTAL_ and syntax and API will likely changed. Here are a few limitations to be aware of at the moment:
8-
>
9-
> - The xUnit test runner can detect and execute tests in Razor test components, but is not able to distinguish the individual `<Fixture>`'s from each other. They are all executed together, one at the time. The solution is planned, see the [related issue](https://github.com/egil/razor-components-testing-library/issues/4) for details.
10-
> - Go to the [Contribute](/docs/contribute) page for info on how to provide feedback and suggestions.
11-
12-
> **TIP:** Working with and asserting against the rendered component and its output is covered on the [Working with rendered components and fragments](/docs/working-with-rendered-components-and-fragments.html) page.
13-
14-
**Content:**
15-
16-
- [Creating a new Razor test component](#creating-a-new-razor-test-component)
17-
- [Defining tests/fixtures in test components](#defining-testsfixtures-in-test-components)
18-
- [Executing test cases](#executing-test-cases)
3+
> **WARNINIG**: This feature is only supported when using bUnit with xUNit. NUnit and MSTest is planned.
194
20-
**Further reading:**
21-
22-
- [Working with rendered components and fragments](/docs/working-with-rendered-components-and-fragments.html)
23-
- [Semantic HTML markup comparison](/docs/semantic-html-markup-comparison.html)
24-
- [Mocking JsRuntime](/docs/mocking-jsruntime.html)
25-
- [Razor test examples](/docs/razor-test-examples.html)
5+
This pages documents how to do Blazor/Razor component testing from `.razor` files.
266

277
## Creating a new Razor test component
288

29-
To create Razor based tests, we need to create test components. All test components must inherit from `TestComponentBase`, e.g. by adding `@inherits TestComponentBase` to the top of your .razor file. The `TestComponentBase` contains all the logic for rendering components and correctly dispose of renderers, components, and HTML parsers after each test.
9+
To create Razor based tests, we need to create Blazor test components. All test components must inherit from `TestComponentBase`, e.g. by adding `@inherits TestComponentBase` to the top of your .razor file.
3010

3111
For example:
3212

@@ -49,14 +29,12 @@ For example:
4929
You will also need to import a few namespaces to make asserting and mocking possible. They are best placed in an `_Imports.razor` file next to your Razor test components, e.g.:
5030

5131
```cshtml
52-
@using Microsoft.AspNetCore.Components.Web
53-
@using Microsoft.Extensions.DependencyInjection
5432
@using Bunit
5533
@using Bunit.Mocking.JSInterop
5634
@using Xunit
5735
```
5836

59-
> **NOTE:** The `_Imports.razor` has already been created for you if you are using the [Blazor test project template](/docs/creating-a-new-test-project.html).
37+
> **NOTE:** The `_Imports.razor` has already been created for you if you are using the [Blazor test project template](/docs/creating-a-new-bunit-xunit-project.html).
6038
6139
## Defining tests/fixtures in test components
6240

@@ -65,13 +43,12 @@ When you have a Razor test component created, its time to add test cases/fixture
6543
Lets look at what options we have by setting up an empty test case, first the code:
6644

6745
```cshtml
68-
<Fixture Description="MyComponent renders as expected" @* Optional - description is shown in error message if test fails *@
69-
Setup="Setup" @* Optional - method called first *@
70-
SetupAsync="SetupAsync" @* Optional - method called after Setup *@
71-
Test="Test1" @* Optional - method called after Setup/SetupAsync *@
72-
TestAsync="Test1Async" @* Optional - method called after Test *@
73-
Tests="new Action[]{ Test2, Test3 }"> @* Optional - methods are called after Test/TestAsync, one at the time *@
74-
TestsAsync="new Func<Task>[]{ Test2Async, Test3Async }"> @* Optional - methods are called after Tests, one at the time *@
46+
<Fixture Description="MyComponent renders as expected"
47+
Timeout="1000"
48+
Skip="Reason to skip the test"
49+
Setup="Setup
50+
SetupAsync="SetupAsync"
51+
Test="Test1" @* or *@ TestAsync="Test1Async">
7552
<ComponentUnderTest>
7653
<MyComponent />
7754
</ComponentUnderTest>
@@ -85,56 +62,38 @@ Lets look at what options we have by setting up an empty test case, first the co
8562
@code {
8663
// Called first if present when added to the Setup parameter
8764
// on a <Fixture> component (can be named anything)
88-
void Setup()
65+
void Setup(Fixture fixture)
8966
{
9067
// Add services and do other setup work in this method.
91-
Services.AddMockJsRuntime();
68+
fixture.Services.AddMockJsRuntime();
9269
}
9370
9471
// Called after Setup if present when added to the Setup parameter
9572
// on a <Fixture> component (can be named anything)
96-
Task SetupAsync() => Task.CompletedTask;
73+
Task SetupAsync(Fixture fixture) => Task.CompletedTask;
9774
9875
// Called after Setup when added to the Test parameter to a
9976
// <Fixture> component (can be named anything)
100-
void Test1()
77+
void Test1(Fixture fixture)
10178
{
10279
// Renders a MyComponent component and assigns the result to
10380
// a cut variable. CUT is short for Component Under Test.
104-
IRenderedComponent<MyComponent> cut = GetComponentUnderTest<MyComponent>();
81+
IRenderedComponent<MyComponent> cut = fixture.GetComponentUnderTest<MyComponent>();
10582
10683
// Renders the markup in the "first" fragment by calling GetFragment without an id.
107-
IRenderedFragment firstFragment = GetFragment();
84+
IRenderedFragment firstFragment = fixture.GetFragment();
10885
10986
// Renders the markup in the "first" fragment by calling GetFragment with an id.
110-
IRenderedFragment alsoFirstFragment = GetFragment("first");
87+
IRenderedFragment alsoFirstFragment = fixture.GetFragment("first");
11188
11289
// Both first fragments refers to the same instance.
11390
Assert.Equal(firstFragment, alsoFirstFragment);
11491
11592
// Renders a MyOtherComponent component defined in the second fragment.
116-
IRenderedComponent<MyOtherComponent> myOtherComponent = GetFragment<MyOtherComponent>("second");
93+
IRenderedComponent<MyOtherComponent> myOtherComponent = fixture.GetFragment<MyOtherComponent>("second");
11794
}
11895
119-
Task Test1Async() => Task.CompletedTask;
120-
121-
// Called after Test when added to the Tests parameter to a
122-
// <Fixture> component (can be named anything). Methods in
123-
// the Tests parameter is called in the order they are present in the
124-
// array.
125-
void Test2()
126-
{
127-
// do more testing on CUT, f1 and f2 by retriving them.
128-
}
129-
130-
void Test3()
131-
{
132-
// do more testing on CUT, f1 and f2 by retriving them.
133-
}
134-
135-
Task Test2Async() => Task.CompletedTask;
136-
137-
Task Test3Async() => Task.CompletedTask;
96+
Task Test1Async(Fixture fixture) => Task.CompletedTask;
13897
}
13998
```
14099

@@ -144,13 +103,12 @@ The code above works as follows:
144103

145104
1. `Setup`
146105
2. `SetupAsync`
147-
3. `Test`
148-
4. `TestAsync`
149-
5. `Tests`, one at the time, in the order they appear in the array.
150-
6. `TestsAsync`, one at the time, in the order they appear in the array.
106+
3. `Test` or `TestAsync`
151107

152-
- The `Description` parameter on the `<Fixture>` element is displayed in the test runners error window if the test fails.
108+
- If the `Description` parameter is present, it is used as the name of the test in the Test Explorer and is displayed in the test runners error window if the test fails.
153109
- It is inside child component `<ComponentUnderTest>` where you declare the component under test.
110+
- If the `Timeout` parameter is specified, the test will timeout after the specified time, in milliseconds.
111+
- If `Skip` parameter is specified, the test is skipped and the reason is printed in the test output.
154112
- Any markup or component fragments that is needed for the test can be declared inside the optional `<Fragment>` components. The `Id` parameter is optional, and only needed if you have more than one.
155113

156114
- To render and get the component under test or any of the fragments, use the `GetComponentUnderTest<TComponent>()` method, where `TComponent` is the type of the component you have defined under the `<ComponentUnderTest>` element.
@@ -163,4 +121,8 @@ The code above works as follows:
163121

164122
Since Blazor test component use xUnit under the hood as a test runner, you execute your tests them in exactly the same way as you would normal xUnit unit tests, i.e. by running `dotnet test` from the console or running the tests through the Test Explorer in Visual Studio.
165123

166-
Do note the current limitations mentioned at the top of the page.
124+
## Further reading:
125+
126+
- [Semantic HTML markup comparison](/docs/semantic-html-markup-comparison.html)
127+
- [Mocking JsRuntime](/docs/mocking-jsruntime.html)
128+
- [Razor based test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/RazorTestComponents)

docs/docs/razor-test-examples.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Razor test examples
22

3+
> **WARNING:** These examples are somewhat outdated. Some content in here might still apply:
4+
35
In the following examples, the terminology **component under test** (abbreviated CUT) is used to mean the component that is the target of the test. The examples below use the `Shouldly` assertion library as well. If you prefer not to use that just replace the assertions with the ones from your own favorite assertion library.
46

5-
All examples can be found in the [Tests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/razor-components-testing-library/tree/master/sample/).
7+
All examples can be found in the [Tests](https://github.com/egil/bunit/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/bunit/tree/master/sample/).
68

79
## Examples
810

9-
Here is a few examples that demonstrate how Razor test components can be used. More can be found in the [sample/tests/RazorComponentTests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/RazorComponentTests) samples folder.
11+
Here is a few examples that demonstrate how Razor test components can be used. More can be found in the [sample/tests/RazorComponentTests](https://github.com/egil/bunit/tree/master/sample/tests/RazorComponentTests) samples folder.
1012

1113
```cshtml
1214
<Fixture Test="ThemedButtonUsesNamedCascadingValue">
@@ -30,7 +32,7 @@ Here is a few examples that demonstrate how Razor test components can be used. M
3032
}
3133
```
3234

33-
This example shows how [ThemedElement.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Components/ThemedElement.razor) can be tested with cascading values.
35+
This example shows how [ThemedElement.razor](https://github.com/egil/bunit/tree/master/sample/src/Components/ThemedElement.razor) can be tested with cascading values.
3436

3537
```cshtml
3638
<Fixture Test=MarkupPassedViaChildContent>
@@ -53,9 +55,9 @@ This example shows how [ThemedElement.razor](https://github.com/egil/razor-compo
5355
}
5456
```
5557

56-
This example shows how [ThemedButton.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Components/ThemedButton.razor) can be tested with with child content, and how a `<Fragment>` can be used to specify the expected output.
58+
This example shows how [ThemedButton.razor](https://github.com/egil/bunit/tree/master/sample/src/Components/ThemedButton.razor) can be tested with with child content, and how a `<Fragment>` can be used to specify the expected output.
5759

58-
Lets look at a more complex example, a test of the [TodoList.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Pages/TodoList.razor) component:
60+
Lets look at a more complex example, a test of the [TodoList.razor](https://github.com/egil/bunit/tree/master/sample/src/Pages/TodoList.razor) component:
5961

6062
```cshtml
6163
<Fixture Setup="() => Services.AddMockJsRuntime()"

docs/docs/semantic-html-markup-comparison.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ On this page we will go through how the comparison works, and what options you h
66

77
> **NOTE:** The semantic HTML comparison is available in all three test types, but is always used in the Snapshot test type.
88
9-
**Content:**
10-
11-
- [Why semantic comparison is needed for stable tests](#why-semantic-comparison-is-needed-for-stable-tests)
12-
- [Customizing the comparison process](#customizing-the-comparison-process)
13-
- [Verifying output from components](#verifying-output-from-components)
14-
- [Different ways of getting the differences](#different-ways-of-getting-the-differences)
15-
169
## Why semantic comparison is needed for stable tests
1710

1811
Just performing string comparison of two strings containing HTML markup can break quite easily, _even_ if the two markup strings are semantically equivalent. Some changes that can cause a regular string comparison to fail are:

0 commit comments

Comments
 (0)