Skip to content

Commit 744f227

Browse files
committed
test: 更新单元测试
1 parent aa63994 commit 744f227

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

test/UnitTest/Components/TitleTest.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,14 @@ namespace UnitTest.Components;
88
public class TitleTest : BootstrapBlazorTestBase
99
{
1010
[Fact]
11-
public void ChildContent_Ok()
11+
public async Task SetTitle_Ok()
1212
{
1313
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
1414
{
15-
pb.AddChildContent<MockTitleTest>();
16-
});
17-
cut.InvokeAsync(async () =>
18-
{
19-
var titleService = cut.FindComponent<MockTitleTest>().Instance.TitleService;
20-
await titleService.SetTitle("test");
21-
});
22-
23-
cut.InvokeAsync(() =>
24-
{
25-
var title = cut.FindComponent<Title>();
26-
title.SetParametersAndRender();
27-
28-
// 模拟 Module 为空
29-
var moduleProperty = title.Instance.GetType().GetProperty("Module", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
30-
moduleProperty?.SetValue(title.Instance, null);
31-
32-
var methodInfo = title.Instance.GetType().GetMethod("SetTitle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
33-
methodInfo?.Invoke(title.Instance, new object[] { new TitleOption() { Title = "test" } });
15+
pb.AddChildContent<MockTitle>();
3416
});
17+
var mockTitle = cut.FindComponent<MockTitle>();
18+
await mockTitle.Instance.SetTitle("test");
3519
}
3620

3721
[Fact]
@@ -45,10 +29,12 @@ public void Text_Ok()
4529
Assert.Equal("Text", text);
4630
}
4731

48-
private class MockTitleTest : ComponentBase
32+
private class MockTitle : ComponentBase
4933
{
5034
[Inject]
5135
[NotNull]
5236
public TitleService? TitleService { get; set; }
37+
38+
public Task SetTitle(string title) => TitleService.SetTitle(title);
5339
}
5440
}

0 commit comments

Comments
 (0)