Skip to content

Commit cf58830

Browse files
committed
Part 3 I guess?
1 parent a9da557 commit cf58830

File tree

10 files changed

+24
-41
lines changed

10 files changed

+24
-41
lines changed

docs/samples/tests/mstest/HelloWorldRazorTest.razor

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
@attribute [TestClass]
2+
@inherits TestContext
23
@code
34
{
45
[TestMethod]
56
public void HelloWorldComponentRendersCorrectly()
67
{
7-
// Arrange
8-
using var ctx = new Bunit.TestContext();
9-
108
// Act
119
var cut = Render(@<HelloWorld/>);
1210

docs/samples/tests/mstest/HelloWorldTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
namespace Bunit.Docs.Samples
55
{
66
[TestClass]
7-
public class HelloWorldTest
7+
public class HelloWorldTest : TestContext
88
{
99
[TestMethod]
1010
public void HelloWorldComponentRendersCorrectly()
1111
{
12-
// Arrange
13-
using var ctx = new Bunit.TestContext();
14-
1512
// Act
1613
var cut = RenderComponent<HelloWorld>();
1714

docs/samples/tests/nunit/HelloWorldTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33

44
namespace Bunit.Docs.Samples
55
{
6-
public class HelloWorldTest
6+
public class HelloWorldTest : TestContext
77
{
88
[Test]
99
public void HelloWorldComponentRendersCorrectly()
1010
{
11-
// Arrange
12-
using var ctx = new Bunit.TestContext();
13-
1411
// Act
1512
var cut = RenderComponent<HelloWorld>();
1613

docs/samples/tests/xunit/UserRightsTest.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public void Test003()
6363
public void Test004()
6464
{
6565
// Arrange
66-
using var ctx = new TestContext();
6766
var authContext = AddTestAuthorization();
6867
authContext.SetAuthorized("TEST USER");
6968
authContext.SetPolicies("content-editor");
@@ -82,7 +81,6 @@ public void Test004()
8281
public void Test0041()
8382
{
8483
// Arrange
85-
using var ctx = new TestContext();
8684
var authContext = AddTestAuthorization();
8785
authContext.SetAuthorized("TEST USER");
8886
authContext.SetPolicies("content-editor", "approver");
@@ -101,7 +99,6 @@ public void Test0041()
10199
public void Test006()
102100
{
103101
// Arrange
104-
using var ctx = new TestContext();
105102
var authContext = AddTestAuthorization();
106103
authContext.SetAuthorized("TEST USER");
107104
authContext.SetClaims(
@@ -124,7 +121,6 @@ public void Test006()
124121
public void Test005()
125122
{
126123
// Arrange
127-
using var ctx = new TestContext();
128124
var authContext = AddTestAuthorization();
129125
authContext.SetAuthorized("TEST USER");
130126
authContext.SetRoles("admin", "superuser");
@@ -148,7 +144,6 @@ public void Test005()
148144
public void Test007()
149145
{
150146
// Arrange
151-
using var ctx = new TestContext();
152147
var authContext = AddTestAuthorization();
153148
authContext.SetAuthorized("TEST USER");
154149
authContext.SetAuthenticationType("custom-auth-type");

docs/site/docs/test-doubles/emulating-ijsruntime.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ Since the .NET 5 release of Blazor, it has been possible to import JavaScript mo
8484
The `SetupModule` methods return a module JSInterop, which can be configured to handle JavaScript calls using the `Setup` and `SetupVoid` methods. For example, to configure bUnit's JSInterop to handle an import of the JavaScript module `hello.js`, and a call to the function `world()` in that model, do the following:
8585

8686
```csharp
87-
using var ctx = new TestContext();
88-
8987
var moduleInterop = JSInterop.SetupModule("hello.js");
9088
moduleInterop.SetupVoid("world");
9189
```

docs/site/docs/test-doubles/fake-navigation-manager.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ As `<a href>` navigation is not natively supported in bUnit, the `NavigationMana
123123
The test utilizes the `NavigationManager` itself to achieve the same:
124124

125125
```csharp
126-
using var ctx = new TestContext();
127126
var navMan = Services.GetRequiredService<FakeNavigationManager>();
128127
var cut = RenderComponent<InterceptAHRefComponent>();
129128

@@ -150,7 +149,6 @@ NavigationManager.NavigateToLogin("authentication/login", requestOptions);
150149

151150
A test could look like this:
152151
```csharp
153-
using var ctx = new TestContext();
154152
var navigationManager = Services.GetRequiredService<FakeNavigationManager>();
155153

156154
ActionToTriggerTheNavigationManager();

docs/site/docs/test-doubles/faking-auth.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The following subsections demonstrate how to set the `<UserInfo>` into all three
3434

3535
To set the state to unauthenticated and unauthorized, do the following:
3636

37-
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=11&end=20&highlight=3)]
37+
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=11&end=19&highlight=2)]
3838

3939
The highlighted line shows how `AddTestAuthorization()` is used to add the test-specific implementation of Blazor's authentication and authorization types to the `Services` collection, which makes the authentication state available to other services as well as components used throughout the test that require it.
4040

@@ -44,23 +44,23 @@ After calling `AddTestAuthorization()`, the default authentication state is unau
4444

4545
To set the state to authenticating and authorizing, do the following:
4646

47-
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=26&end=36&highlight=4)]
47+
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=25&end=34&highlight=3)]
4848

4949
After calling `AddTestAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticating and authorizing state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorizing> method.
5050

5151
### Authenticated and unauthorized state
5252

5353
To set the state to authenticated and unauthorized, do the following:
5454

55-
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=42&end=52&highlight=4)]
55+
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=40&end=49&highlight=3)]
5656

5757
After calling `AddTestAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticated and unauthorized state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
5858

5959
### Authenticated and authorized state
6060

6161
To set the state to authenticated and authorized, do the following:
6262

63-
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=58&end=68&highlight=4)]
63+
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=55&end=64&highlight=3)]
6464

6565
After calling `AddTestAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticated and authorized state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
6666

@@ -78,17 +78,17 @@ The examples will use the `<UserRights>` component listed below. It uses the `<A
7878

7979
To specify one or more roles for the authenticated and authorized user, do the following:
8080

81-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=29&end=42&highlight=5)]
81+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=28&end=40&highlight=4)]
8282

8383
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetRoles(System.String[])> method is used to specify a single role. To specify multiple roles, do the following:
8484

85-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=48&end=62&highlight=5)]
85+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=46&end=59&highlight=4)]
8686

8787
### Policies
8888

8989
To specify one or more policies for the authenticated and authorized user, do the following:
9090

91-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=68&end=81&highlight=5)]
91+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=65&end=78&highlight=5)]
9292

9393
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetPolicies(System.String[])> method is used to specify one policy. To specify multiple policies, do the following:
9494

@@ -98,22 +98,22 @@ The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationCont
9898

9999
To specify one or more claims for the authenticated and authorized user, do the following:
100100

101-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=106&end=123&highlight=5-8)]
101+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=101&end=117&highlight=4-7)]
102102

103103
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetClaims(System.Security.Claims.Claim[])> method is used to pass two instances of the `Claim` type.
104104

105105
### Example of passing both roles, claims, and policies
106106

107107
Let’s try to combine all the possibilities shown in the previous examples into one. The following example specifies two roles, one claim, and one policy for the authenticated and authorized user:
108108

109-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=129&end=147&highlight=4-8)]
109+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=123&end=140&highlight=3-7)]
110110

111111
With this example done, all auth-related test scenarios should be covered. If you find that one is missing, please let us know in the [bUnit discussion forum](https://github.com/egil/bUnit/discussions).
112112

113113
### Authentication types
114114

115115
To specify a authentication type for the authenticated and authorized user, do the following:
116116

117-
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=153&end=166&highlight=5)]
117+
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=146&end=158&highlight=4)]
118118

119119
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthenticationType(System.String)> method is used to change the `Identity.AuthenticationType` of the user.

docs/site/docs/verification/async-assertion.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Let's look at an example. Consider the following `<AsyncData>` component, which
2626

2727
To test the `<AsyncData>` component, do the following:
2828

29-
[!code-csharp[AsyncDataTest.cs](../../../samples/tests/xunit/AsyncDataTest.cs?start=54&end=65&highlight=3,9,12)]
29+
[!code-csharp[AsyncDataTest.cs](../../../samples/tests/xunit/AsyncDataTest.cs?start=52&end=62&highlight=2,8,11)]
3030

3131
This is what happens in the test:
3232

@@ -38,7 +38,7 @@ This is what happens in the test:
3838

3939
The timeout, which defaults to one second, can be controlled by passing a `TimeSpan` as the second argument to the `WaitForAssertion()` method, e.g.:
4040

41-
[!code-csharp[](../../../samples/tests/xunit/AsyncDataTest.cs?start=66&end=66)]
41+
[!code-csharp[](../../../samples/tests/xunit/AsyncDataTest.cs?start=63&end=63)]
4242

4343
If the timeout is reached, a <xref:Bunit.Extensions.WaitForHelpers.WaitForFailedException> exception is thrown with the following error message:
4444

docs/site/docs/verification/semantic-html-comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ Let’s look at a few examples where we use the semantic comparison options list
143143

144144
In this case, we want to verify that the markup is rendered correctly, using something such as RegEx to verify the `id` attribute (it might be generated) and ignoring the `<small>` element. In tests we can do this like so with the `MarkupMatches()` method:
145145

146-
[!code-csharp[SemanticHtmlTest.cs](../../../samples/tests/xunit/SemanticHtmlTest.cs#L16-L29)]
146+
[!code-csharp[SemanticHtmlTest.cs](../../../samples/tests/xunit/SemanticHtmlTest.cs#L16-L28)]

docs/site/docs/verification/verify-markup.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ To access the rendered markup of a component, just use the <xref:Bunit.IRendered
3030
3131
To get the markup as a string, do the following:
3232

33-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=16&end=21&highlight=5)]
33+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=16&end=19&highlight=3)]
3434

3535
You can perform standard string assertions against the markup string, like checking whether it contains a value or is empty.
3636

@@ -70,13 +70,13 @@ In the following examples, the `<Heading>` component listed below will be used a
7070

7171
To use the `MarkupMatches()` method to perform a semantic comparison of the output of the `<Heading>` component through its <xref:Bunit.IRenderedFragment>, do the following:
7272

73-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=27&end=34&highlight=5-9)]
73+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=25&end=30&highlight=3-6)]
7474

7575
The highlighted line shows the call to the `MarkupMatches()` method. This test passes even though the insignificant whitespace is not exactly the same between the expected HTML string and the raw markup produced by the `<Heading>` component. It even works when the CSS class list is not in the same order on the `<small>` element.
7676

7777
The `MarkupMatches()` method is also available on `INode` and `INodeList` types, for example:
7878

79-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=40&end=45&highlight=5-6)]
79+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=36&end=39&highlight=3-4)]
8080

8181
Here we use the `Find(string cssSelector)` method to find the `<small>` element, and only verify it and its content and attributes.
8282

@@ -85,7 +85,7 @@ Here we use the `Find(string cssSelector)` method to find the `<small>` element,
8585
8686
Text content can also be verified with the `MarkupMatches()` method, e.g. the text inside the `<small>` element. It has the advantage over regular string comparison in that it removes insignificant whitespace in the text automatically - even between words - where a normal string `Trim()` method isn't enough. For example:
8787

88-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=51&end=56&highlight=5)]
88+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=45&end=48&highlight=3)]
8989

9090
The semantic HTML comparer can be customized to make a test case even more stable and easier to maintain. For example, it is possible to ignore an element or attribute during comparison, or provide a regular expression to the comparer when comparing a specific element or attribute to make the comparer work with generated data.
9191

@@ -112,11 +112,11 @@ Let's see some examples of using the [`Find(string cssSelector)`](xref:Bunit.Ren
112112

113113
To find the `<caption>` element and the first `<td>` elements in each row, do the following:
114114

115-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=62&end=67&highlight=5-6)]
115+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=54&end=57&highlight=3-4)]
116116

117117
Once you have one or more elements, you verify against them, such as by inspecting their properties through the DOM API. For example:
118118

119-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=69&end=71)]
119+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=59&end=61)]
120120

121121
#### Auto-refreshing Find() queries
122122

@@ -148,7 +148,7 @@ Let's look at a few examples of using the assertion helpers. In the first one,
148148

149149
Here is an example of using the <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender> method:
150150

151-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=77&end=90&highlight=8,11,14)]
151+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=67&end=79&highlight=7,10,13)]
152152

153153
This is what happens in the test:
154154

@@ -164,7 +164,7 @@ This example tests the `<CheckList>` component listed below. The component allow
164164

165165
To test the end-to-end life cycle of adding and removing items from the `<CheckList>` component, do the following:
166166

167-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=96&end=141)]
167+
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=85&end=126)]
168168

169169
This is what happens in the test:
170170

0 commit comments

Comments
 (0)