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
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Writing Blazor Component tests in C#
2
2
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.
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
5
All examples can be found in the [Tests](../sample/tests/Tests) folder in the [Sample project](../sample/).
6
6
@@ -135,9 +135,9 @@ A few things worth noting about the tests above:
135
135
2. The "**strict**" test (`ClickingButtonIncreasesCountStrict`) and the "**targeted**" test (`ClickingButtonIncreasesCountTargeted`) takes two different approaches to verifying CUT renders the expected output:
136
136
137
137
- The **strict** version generates a diff between the initial rendered HTML and the rendered HTML after the button click, and then asserts that the compare result only contains the expected change.
138
-
- The **targeted** version finds the `<p>` element expect to have changed, and asserts against its text content.
138
+
- The **targeted** version finds the `<p>` element expect to have changed and asserts against its text content.
139
139
140
-
With the _targeted_ version, we cannot guarantee that there are not other changes in other places of the rendered HTML, if that is a concern, use the strict style. If it is not, then the targeted style can lead to simpler test.
140
+
With the _targeted_ version, we cannot guarantee that there are no other changes in other places of the rendered HTML, if that is a concern, use the strict style. If it is not, then the targeted style can lead to simpler test.
141
141
142
142
## Testing components with parameters
143
143
@@ -198,7 +198,7 @@ In the test above, we use an overload of the `RenderComponent<TComponent>()` met
198
198
199
199
As highlighted in the code, I recommend using the [`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) to get the name of declared parameters from the component, so any changes to the name through refactoring automatically updates the test.
200
200
201
-
The second parameter, `class` is not explicitly declared in the `Aside` class. It is instead the `Attributes` parameter, that captures all unmatched parameters.
201
+
The second parameter, `class` is not explicitly declared in the `Aside` class. It is instead the `Attributes` parameter, that captures it and all other unmatched parameters.
202
202
203
203
### Passing new parameters to an already rendered component
204
204
@@ -244,7 +244,7 @@ Some notes on `Test002` above:
244
244
245
245
## Testing components with child content
246
246
247
-
The [Aside.razor](../sample/src/Components/Aside.razor) component listed in the previous section also has a `ChildContent` parameter, so lets add a few tests that passes markup and components to it through that.
247
+
The [Aside.razor](../sample/src/Components/Aside.razor) component listed in the previous section also has a `ChildContent` parameter, so let's add a few tests that passes markup and components to it through that.
248
248
249
249
```csharp
250
250
publicclassAsideTest : ComponentTestFixture
@@ -430,11 +430,11 @@ public class ThemedButtonTest : ComponentTestFixture
430
430
431
431
It is not uncommon to have components use Blazor's JSInterop functionality to call JavaScript, e.g. after first render.
432
432
433
-
To make it easy to mock calls to JavaScript, the library comes with a`IJsRuntime` mocking helper, that allows you to specify return how JSInterop calls should be handled, and to verify that they have happened.
433
+
To make it easy to mock calls to JavaScript, the library comes with an`IJsRuntime` mocking helper, that allows you to specify return how JSInterop calls should be handled, and to verify that they have happened.
434
434
435
435
If you have more complex mocking needs, you could look to frameworks like [Moq](https://github.com/Moq).
436
436
437
-
To help us test the Mock JSRuntime, we have the [WikiSearch.razor](../sample/src/Components/WikiSearch.razor) component, which looks like this:
437
+
To help demonstrate testing using the mock JSRuntime, we have the [WikiSearch.razor](../sample/src/Components/WikiSearch.razor) component, which looks like this:
438
438
439
439
```cshtml
440
440
@inject IJSRuntime jsRuntime
@@ -473,7 +473,7 @@ public class WikiSearchTest : ComponentTestFixture
473
473
// Arrange
474
474
// Registered the MockJsRuntime in "Loose" mode with the service provider used when rendering components.
475
475
// JsRuntimeMockMode.Loose is the default. It configures the mock to just return the default
476
-
// value for whatever is requested in a InvokeAsync call if no call has explicitly been set up.
476
+
// value for whatever is requested in an InvokeAsync call if no call has explicitly been set up.
477
477
varjsMock=Services.AddMockJsRuntime();
478
478
479
479
// Act - render the WikiSearch component
@@ -516,7 +516,7 @@ public class WikiSearchTest : ComponentTestFixture
516
516
}
517
517
```
518
518
519
-
-`Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync<TValue>(...)` it receives. This allows us to test components that expects a`IJsRuntime` to be injected, but where the test we want to perform isn't dependent on it providing any specific return value.
519
+
-`Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync<TValue>(...)` it receives. This allows us to test components that expects an`IJsRuntime` to be injected, but where the test we want to perform isn't dependent on it providing any specific return value.
520
520
521
521
In "Loose" mode it is still possible to call `VerifyInvoke(identifier)` and assert against the expected invocation.
522
522
@@ -527,7 +527,7 @@ public class WikiSearchTest : ComponentTestFixture
527
527
528
528
### Verifying element references passed to InvokeAsync
529
529
530
-
If you want to verify that a element reference (`ElementReference`) passed to a IJsRuntime.InvokeAsync call is references the expected DOM element, you can do so with the `ShouldBeElementReferenceTo()` assert helper.
530
+
If you want to verify that an element reference (`ElementReference`) passed to a IJsRuntime.InvokeAsync call is references the expected DOM element, you can do so with the `ShouldBeElementReferenceTo()` assert helper.
531
531
532
532
For example, consider the [FocussingInput.razor](../sample/src/Components/FocussingInput.razor) component, which looks like this:
533
533
@@ -586,7 +586,7 @@ The demonstrate service injection, lets refactor the [FetchData.razor](../sample
586
586
587
587
- Extract an interface from [WeatherForecastService](../sample/src/Data/WeatherForecastService.cs), name it [IWeatherForecastService](../sample/src/Data/IWeatherForecastService.cs), and have `FetchData` take a dependency on it.
588
588
589
-
- Extract the `<table>` inside the `else` branch in the [FetchData.razor](../sample/src/Pages/FetchData.razor) component into its own component. Lets name it [ForecastDataTable](../sample/src/Pages/FetchData.razor).
589
+
- Extract the `<table>` inside the `else` branch in the [FetchData.razor](../sample/src/Pages/FetchData.razor) component into its own component. Let's name it [ForecastDataTable](../sample/src/Pages/FetchData.razor).
590
590
591
591
- In the [FetchData.razor](../sample/src/Pages/FetchData.razor), pass the variable `forecasts` to the [ForecastDataTable](../sample/src/Pages/FetchData.razor) component.
592
592
@@ -650,9 +650,9 @@ public class FetchDataTest : ComponentTestFixture
650
650
651
651
- In `Test001` we use the `Services.AddService` method to register the dependency and the performs a regular "initial render" verification.
652
652
653
-
-`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.
653
+
-`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.
654
654
655
-
- 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.
655
+
- In the assert step we expect the CUT to use a `ForecastDataTable` to render the forecast data. Thus, to make our assertion simpler and more 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.
0 commit comments