Skip to content

Commit 31057f0

Browse files
csharpfritzCopilothishamcodependabot[bot]
authored
V0.13 - Updated to .NET 10 and Copilot Introduction (#292)
Co-authored-by: csharpfritz <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Jeffrey T. Fritz <[email protected]> Co-authored-by: hishamco <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]> Fixed tests (#275) Fixing spaces in YML (#278)
1 parent ff753cc commit 31057f0

File tree

490 files changed

+19221
-6706
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

490 files changed

+19221
-6706
lines changed

.github/copilot-instructions.md

Lines changed: 486 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
---
2+
name: bunit-test-migration
3+
description: Migrate bUnit test files from deprecated beta API (1.0.0-beta-10) to bUnit 2.x stable API. Use this when working on .razor test files in BlazorWebFormsComponents.Test that contain old patterns like TestComponentBase, Fixture, or SnapshotTest.
4+
---
5+
6+
# bUnit Test Migration Skill (Beta → 2.x)
7+
8+
This skill provides guidance for migrating test files from the deprecated bUnit 1.0.0-beta-10 API to bUnit 2.5.3 stable API. Use this when you encounter test files using the old `TestComponentBase`, `<Fixture>`, or `<SnapshotTest>` patterns.
9+
10+
## When to Apply
11+
12+
Apply this skill when a `.razor` test file contains any of these patterns:
13+
- `@inherits TestComponentBase`
14+
- `<Fixture Test="...">`
15+
- `<ComponentUnderTest>`
16+
- `<SnapshotTest>`
17+
- `void MethodName(Fixture fixture)`
18+
19+
## Transformation Rules
20+
21+
### 1. Change Inheritance
22+
23+
```diff
24+
- @inherits TestComponentBase
25+
+ @inherits BunitContext
26+
```
27+
28+
### 2. Remove Wrapper Elements
29+
30+
Remove these XML elements entirely (keep only the component inside):
31+
32+
```diff
33+
- <Fixture Test="TestName">
34+
- <ComponentUnderTest>
35+
<MyComponent Parameter="value" />
36+
- </ComponentUnderTest>
37+
- </Fixture>
38+
```
39+
40+
### 3. Convert Test Methods
41+
42+
```diff
43+
- void TestMethodName(Fixture fixture)
44+
+ [Fact]
45+
+ public void ComponentName_Scenario_ExpectedResult()
46+
```
47+
48+
### 4. Replace Component Access
49+
50+
```diff
51+
- var cut = fixture.GetComponentUnderTest();
52+
+ var cut = Render(@<MyComponent Parameter="value" />);
53+
```
54+
55+
### 5. Convert Snapshot Tests
56+
57+
```diff
58+
- <SnapshotTest Description="renders correctly">
59+
- <TestInput>
60+
- <MyComponent />
61+
- </TestInput>
62+
- <ExpectedOutput>
63+
- <div>expected html</div>
64+
- </ExpectedOutput>
65+
- </SnapshotTest>
66+
+ [Fact]
67+
+ public void MyComponent_Default_RendersCorrectly()
68+
+ {
69+
+ var cut = Render(@<MyComponent />);
70+
+ cut.MarkupMatches(@<div>expected html</div>);
71+
+ }
72+
```
73+
74+
## Complete Example
75+
76+
### Before
77+
78+
```razor
79+
@inherits TestComponentBase
80+
81+
<Fixture Test="ShouldClickButton">
82+
<ComponentUnderTest>
83+
<Button OnClick="OnClick">Click me</Button>
84+
</ComponentUnderTest>
85+
</Fixture>
86+
87+
@code {
88+
int ClickCount = 0;
89+
90+
void ShouldClickButton(Fixture fixture)
91+
{
92+
var cut = fixture.GetComponentUnderTest();
93+
94+
cut.Find("button").Click();
95+
96+
ClickCount.ShouldBe(1);
97+
}
98+
99+
void OnClick() => ClickCount++;
100+
}
101+
```
102+
103+
### After
104+
105+
```razor
106+
@inherits BunitContext
107+
108+
@code {
109+
int ClickCount = 0;
110+
111+
[Fact]
112+
public void Button_Click_IncrementsCounter()
113+
{
114+
var cut = Render(@<Button OnClick="OnClick">Click me</Button>);
115+
116+
cut.Find("button").Click();
117+
118+
ClickCount.ShouldBe(1);
119+
}
120+
121+
void OnClick() => ClickCount++;
122+
}
123+
```
124+
125+
## Test Naming Convention
126+
127+
Pattern: `ComponentName_Scenario_ExpectedResult`
128+
129+
| Component | Scenario | Result | Test Name |
130+
|-----------|----------|--------|-----------|
131+
| Button | Click | InvokesHandler | `Button_Click_InvokesHandler` |
132+
| DataList | EmptySource | ShowsEmptyTemplate | `DataList_EmptySource_ShowsEmptyTemplate` |
133+
| GridView | WithData | RendersRows | `GridView_WithData_RendersRows` |
134+
135+
## Special Patterns
136+
137+
### Multiple Tests in One File
138+
139+
Each `<Fixture>` block becomes a separate `[Fact]` method:
140+
141+
```razor
142+
@inherits BunitContext
143+
144+
@code {
145+
[Fact]
146+
public void Component_FirstScenario_ExpectedResult() { ... }
147+
148+
[Fact]
149+
public void Component_SecondScenario_ExpectedResult() { ... }
150+
}
151+
```
152+
153+
### Tests with Services
154+
155+
```razor
156+
@code {
157+
[Fact]
158+
public void Component_WithService_Works()
159+
{
160+
Services.AddSingleton<IMyService>(new FakeService());
161+
162+
var cut = Render(@<MyComponent />);
163+
}
164+
}
165+
```
166+
167+
### Authentication Tests
168+
169+
```razor
170+
@code {
171+
[Fact]
172+
public void SecureComponent_AuthenticatedUser_ShowsContent()
173+
{
174+
var authContext = this.AddTestAuthorization();
175+
authContext.SetAuthorized("TestUser");
176+
authContext.SetRoles("Admin");
177+
178+
var cut = Render(@<SecureComponent />);
179+
}
180+
}
181+
```
182+
183+
### Tests Requiring New TestContext
184+
185+
For tests that need isolated context (e.g., multiple renders):
186+
187+
```razor
188+
@code {
189+
[Fact]
190+
public void Component_MultipleRenders_WorksCorrectly()
191+
{
192+
using var ctx = new Bunit.TestContext();
193+
194+
var cut1 = ctx.Render(@<MyComponent Value="1" />);
195+
var cut2 = ctx.Render(@<MyComponent Value="2" />);
196+
197+
cut1.Find("span").TextContent.ShouldBe("1");
198+
cut2.Find("span").TextContent.ShouldBe("2");
199+
}
200+
}
201+
```
202+
203+
## Quick Reference Table
204+
205+
| Old Pattern | New Pattern |
206+
|-------------|-------------|
207+
| `@inherits TestComponentBase` | `@inherits BunitContext` |
208+
| `<Fixture Test="Name">` | Remove |
209+
| `<ComponentUnderTest>` | Remove |
210+
| `<SnapshotTest>` | `[Fact]` method with `MarkupMatches()` |
211+
| `void Name(Fixture fixture)` | `[Fact] public void Name()` |
212+
| `fixture.GetComponentUnderTest()` | `Render(@<Component />)` |
213+
| `fixture.GetComponentUnderTest<T>()` | `Render<T>(@<Component />)` |
214+
215+
## Verification
216+
217+
After migrating a file, verify with:
218+
219+
```powershell
220+
# Build check
221+
dotnet build src/BlazorWebFormsComponents.Test --no-restore
222+
223+
# List discovered tests
224+
dotnet test src/BlazorWebFormsComponents.Test --list-tests --filter "FullyQualifiedName~ComponentName"
225+
226+
# Run tests
227+
dotnet test src/BlazorWebFormsComponents.Test --filter "FullyQualifiedName~ComponentName"
228+
```
229+
230+
## Common Errors
231+
232+
| Error | Cause | Fix |
233+
|-------|-------|-----|
234+
| `CS0246: TestComponentBase not found` | Old inheritance | Change to `@inherits BunitContext` |
235+
| `CS0103: Fixture does not exist` | Old wrapper element | Remove `<Fixture>` tags |
236+
| `No tests discovered` | Missing `[Fact]` attribute | Add `[Fact]` to test methods |
237+
| `Method must be public` | Private test method | Add `public` modifier |

0 commit comments

Comments
 (0)