You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/providing-input/passing-parameters-to-components.md
+38-4Lines changed: 38 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,47 @@ title: Passing Parameters to a Component Under Test
5
5
6
6
# Passing Parameters to a Component Under Test
7
7
8
-
Describe factory methods and builder.
8
+
bUnit comes with a bunch of ways to pass parameters to a component under test.
9
9
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.
11
11
12
-
## Component Specific Parameters
12
+
For C#-based test code, you can:
13
13
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.:
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.
0 commit comments