Skip to content

Commit f6cf960

Browse files
committed
docs: first part of params
1 parent 668746e commit f6cf960

4 files changed

Lines changed: 98 additions & 5 deletions

File tree

docs/docs/providing-input/passing-parameters-to-components.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@ title: Passing Parameters to a Component Under Test
55

66
# Passing Parameters to a Component Under Test
77

8-
Describe factory methods and builder.
8+
bUnit comes with a bunch of ways to pass parameters to a component under test.
99

10-
Highlight blazor test components only need this for re-renders through SetParametersAndRender, which alternatively can be done by just mutating a field captured by a parameter.
10+
In Razor-based tests, those written in `.razor` files, passing parameters is exactly the same as in your normal Blazor pages and components, i.e. through the normal Razor syntax, so this parameter passing style will not be covered here. Instead, this page will cover passing parameters in C# code.
1111

12-
## Component Specific Parameters
12+
For C#-based test code, you can:
1313

14-
Show same cases for factory and builder using tabs.
14+
- use loosely typed factory methods
15+
- use a simple tuple-based syntax, i.e. `(name, value)`
16+
- use a strongly typed builder (still experimental)
17+
18+
There are two methods in bUnit that allows passing parameters:
19+
20+
- `RenderComponent` on the test context
21+
- `SetParametersAndRender` on a rendered component
22+
23+
In the following sub sections we will show each style, just click between them using the tabs.
24+
25+
> [!TIP]
26+
> In all examples below, the <xref:Bunit.ComponentParameterFactory> is imported into the test class using `using static Bunit.ComponentParameterFactory;`. This results in a lot less boilerplate code, which improves test readability.
27+
28+
> [!NOTE]
29+
> The examples below are written using xUnit, but the code is the same with NUnit and MSTest.
30+
31+
## Regular Parameters
32+
33+
A regular parameters is one that is declared using the `[Parameter]` attribute.
34+
35+
First, let's look at an example of passing parameter that takes types which or _not_ special to Blazor, i.e.:
36+
37+
[!code-csharp[AllKindsOfParams.razor](../../samples/components/AllKindsOfParams.razor#L3-L7)]
38+
39+
Using either C# tuples, a factory method or the parameter builder, this can be done like this:
40+
41+
[!code-csharp[AllKindsOfParamsTest.cs](../../samples/tests/xunit/AllKindsOfParamsTest.cs#L17-L39)]
42+
43+
All of these examples does the same thing, here is what is going on:
44+
45+
1. The first example, passes parameters using C# tuples, `(string name, object? value)`.
46+
2. The second example also uses C# tuples to pass the parameters, but the name is retrieved in a refactor safe manner using the `nameof` keyword in C#.
47+
3. The third example uses the <xref:Bunit.ComponentParameterFactory.Parameter(System.String,System.Object)> factory method.
48+
4. The last example uses the <xref:Bunit.ComponentParameterBuilder`1>'s `Add` method, which takes a parameter selector expression that selects the parameter using a lambda, and forces you to provide the correct type for the value. This makes the builders methods strongly typed and refactor safe.
1549

1650
### EventCallback
1751

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@code
2+
{
3+
[Parameter]
4+
public int Numbers { get; set; }
5+
6+
[Parameter]
7+
public List<string> Lines { get; set; }
8+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Xunit;
2+
using Bunit;
3+
using System.Collections.Generic;
4+
5+
using static Bunit.ComponentParameterFactory;
6+
7+
namespace Docs.Components.Xunit
8+
{
9+
10+
public class AllKindsOfParamsTest
11+
{
12+
[Fact]
13+
public void RegularParametersLooselyTyped()
14+
{
15+
using var ctx = new TestContext();
16+
17+
// Using C# tuple with hardcoded name
18+
var cut1 = ctx.RenderComponent<AllKindsOfParams>(
19+
("Numbers", 42),
20+
("Lines", new List<string> { "Hello", "World" })
21+
);
22+
23+
// Using C# tuple with refactor safe name
24+
var cut2 = ctx.RenderComponent<AllKindsOfParams>(
25+
(nameof(AllKindsOfParams.Numbers), 42),
26+
(nameof(AllKindsOfParams.Lines), new List<string> { "Hello", "World" })
27+
);
28+
29+
// Using factory method
30+
var cut3 = ctx.RenderComponent<AllKindsOfParams>(
31+
Parameter("Numbers", 42),
32+
Parameter("Lines", new List<string> { "Hello", "World" })
33+
);
34+
35+
// Using parameter builder
36+
var cut4 = ctx.RenderComponent<AllKindsOfParams>(parameters => parameters
37+
.Add(p => p.Numbers, 42)
38+
.Add(p => p.Lines, new List<string> { "Hello", "World" })
39+
);
40+
}
41+
42+
[Fact]
43+
public void RegularParameters2()
44+
{
45+
using var ctx = new TestContext();
46+
47+
48+
}
49+
}
50+
}

docs/templates/bunit/styles/main.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ code[name] {
9999
}
100100
}
101101

102-
.alert-info code {
102+
.alert-info code,
103+
.alert-info a.xref {
103104
background-color: inherit;
104105
color: #131ed2;
105106
}

0 commit comments

Comments
 (0)