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/csharp-examples.md
+66-7Lines changed: 66 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
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.
4
4
5
-
All examples can be found in the [CodeOnlyTests](../sample/tests/CodeOnlyTests) folder in the [Sample project](../sample/).
5
+
All examples can be found in the [Tests](../sample/tests/Tests) folder in the [Sample project](../sample/).
6
6
7
7
1.[Creating new test classes](#creating-new-test-classes)
8
8
2.[Testing components without parameters](#testing-components-without-parameters)
@@ -14,6 +14,7 @@ All examples can be found in the [CodeOnlyTests](../sample/tests/CodeOnlyTests)
14
14
7.[Testing components that use on IJsRuntime](#testing-components-that-use-on-ijsruntime)
15
15
7.1 [Verifying element references passed to InvokeAsync](#verifying-element-references-passed-to-invokeasync)
16
16
8.[Testing components with injected dependencies](#testing-components-with-injected-dependencies)
17
+
9.[Dispatching @on-events from tests](#Dispatching-on-events-from-tests)
17
18
18
19
## Creating new test classes
19
20
@@ -55,7 +56,7 @@ The following unit-tests verifies that the [Counter.razor](../sample/src/Pages/C
55
56
}
56
57
```
57
58
58
-
The [CounterTest.cs](../sample/tests/CodeOnlyTests/Pages/CounterTest.cs) looks like this:
59
+
The [CounterTest.cs](../sample/tests/Tests/Pages/CounterTest.cs) looks like this:
59
60
60
61
```csharp
61
62
publicclassCounterTest : ComponentTestFixture
@@ -157,7 +158,7 @@ The component under test will be the [Aside.razor](../sample/src/Components/Asid
157
158
}
158
159
```
159
160
160
-
The [AsideTest.cs](../sample/tests/CodeOnlyTests/Components/AsideTest.cs) looks like this:
161
+
The [AsideTest.cs](../sample/tests/Tests/Components/AsideTest.cs) looks like this:
161
162
162
163
```csharp
163
164
publicclassAsideTest : ComponentTestFixture
@@ -329,7 +330,7 @@ To show how to pass an `EventCallback` to a component under test, we will use th
329
330
}
330
331
```
331
332
332
-
The relevant part of [ThemedButtonTest.cs](../sample/tests/CodeOnlyTests/Components/ThemedButtonTest.cs) looks like this:
333
+
The relevant part of [ThemedButtonTest.cs](../sample/tests/Tests/Components/ThemedButtonTest.cs) looks like this:
@@ -594,7 +595,7 @@ internal class MockForecastService : IWeatherForecastService
594
595
}
595
596
```
596
597
597
-
With the mock in place, we can write the [FetchDataTest.cs](../sample/tests/CodeOnlyTests/Pages/FetchDataTest.cs), which looks like this:
598
+
With the mock in place, we can write the [FetchDataTest.cs](../sample/tests/Tests/Pages/FetchDataTest.cs), which looks like this:
598
599
599
600
```csharp
600
601
publicclassFetchDataTest : ComponentTestFixture
@@ -647,3 +648,61 @@ public class FetchDataTest : ComponentTestFixture
647
648
-`Test002` creates a new instance of the mock service and registers that with the the service provider. It then renders the CUT and uses `WaitForNextRender` to pass the test data to the mock services task, which then completes and the CUT gets the data.
648
649
649
650
- In the assert step we expect the CUT to use a `ForecastDataTable` to render the forecast data. Thus, to make our assertion more simple and stable to changes, we render an instance of the `ForecastDataTable` use that to verify that the expected addition after the CUT receives the forecast data is as it should be.
651
+
652
+
## Dispatching `@on-events` during testing
653
+
654
+
In the previous sections we have seen a few examples of method calls that trigger `@on-event` handlers, e.g. `cut.Find(selector).Click()` that triggers the `@onclick` event handler attached to the element that matches the search query.
655
+
656
+
The following triggers are currently available in PascalCase, without the `@on`-prefix. E.g. the `@onbeforeactivate` event is available as `BeforeActivate()` in various overloads. I expect to add the missing events soon.
0 commit comments