Skip to content

Commit 248b149

Browse files
authored
Update csharp-examples.md
1 parent e42b297 commit 248b149

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

docs/csharp-examples.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Writing Blazor Component tests in C#
22

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.
44

55
All examples can be found in the [Tests](../sample/tests/Tests) folder in the [Sample project](../sample/).
66

@@ -135,9 +135,9 @@ A few things worth noting about the tests above:
135135
2. The "**strict**" test (`ClickingButtonIncreasesCountStrict`) and the "**targeted**" test (`ClickingButtonIncreasesCountTargeted`) takes two different approaches to verifying CUT renders the expected output:
136136

137137
- 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.
139139

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.
141141

142142
## Testing components with parameters
143143

@@ -198,7 +198,7 @@ In the test above, we use an overload of the `RenderComponent<TComponent>()` met
198198

199199
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.
200200

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.
202202

203203
### Passing new parameters to an already rendered component
204204

@@ -244,7 +244,7 @@ Some notes on `Test002` above:
244244

245245
## Testing components with child content
246246

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.
248248

249249
```csharp
250250
public class AsideTest : ComponentTestFixture
@@ -430,11 +430,11 @@ public class ThemedButtonTest : ComponentTestFixture
430430

431431
It is not uncommon to have components use Blazor's JSInterop functionality to call JavaScript, e.g. after first render.
432432

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.
434434

435435
If you have more complex mocking needs, you could look to frameworks like [Moq](https://github.com/Moq).
436436

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:
438438

439439
```cshtml
440440
@inject IJSRuntime jsRuntime
@@ -473,7 +473,7 @@ public class WikiSearchTest : ComponentTestFixture
473473
// Arrange
474474
// Registered the MockJsRuntime in "Loose" mode with the service provider used when rendering components.
475475
// 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.
477477
var jsMock = Services.AddMockJsRuntime();
478478

479479
// Act - render the WikiSearch component
@@ -516,7 +516,7 @@ public class WikiSearchTest : ComponentTestFixture
516516
}
517517
```
518518

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.
520520

521521
In "Loose" mode it is still possible to call `VerifyInvoke(identifier)` and assert against the expected invocation.
522522

@@ -527,7 +527,7 @@ public class WikiSearchTest : ComponentTestFixture
527527

528528
### Verifying element references passed to InvokeAsync
529529

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.
531531

532532
For example, consider the [FocussingInput.razor](../sample/src/Components/FocussingInput.razor) component, which looks like this:
533533

@@ -586,7 +586,7 @@ The demonstrate service injection, lets refactor the [FetchData.razor](../sample
586586

587587
- 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.
588588

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).
590590

591591
- In the [FetchData.razor](../sample/src/Pages/FetchData.razor), pass the variable `forecasts` to the [ForecastDataTable](../sample/src/Pages/FetchData.razor) component.
592592

@@ -650,9 +650,9 @@ public class FetchDataTest : ComponentTestFixture
650650

651651
- In `Test001` we use the `Services.AddService` method to register the dependency and the performs a regular "initial render" verification.
652652

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.
654654

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.
656656

657657
## Dispatching `@on-events` during testing
658658

0 commit comments

Comments
 (0)