Skip to content

Commit 8bc3c76

Browse files
committed
test: 更新单元测试
1 parent b92f432 commit 8bc3c76

5 files changed

Lines changed: 74 additions & 37 deletions

File tree

test/UnitTest/Components/BootstrapBlazorRootTest.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@
55

66
//using HarmonyLib;
77

8+
using Bunit.TestDoubles;
9+
810
namespace UnitTest.Components;
911

1012
public class BootstrapBlazorRootTest : TestBase
1113
{
1214
[Fact]
1315
public void Render_Ok()
1416
{
15-
var context = new TestContext();
16-
context.JSInterop.Mode = JSRuntimeMode.Loose;
17-
18-
var sc = context.Services;
19-
sc.AddBootstrapBlazor();
20-
sc.ConfigureJsonLocalizationOptions(op =>
21-
{
22-
op.IgnoreLocalizerMissing = false;
23-
});
24-
sc.AddScoped<IRootComponentGenerator, MockGenerator>();
25-
var cut = context.RenderComponent<BootstrapBlazorRoot>();
17+
Context.Services.AddBootstrapBlazor();
18+
Context.Services.AddScoped<IRootComponentGenerator, MockGenerator>();
19+
Context.Services.GetRequiredService<ICacheManager>();
20+
var cut = Context.RenderComponent<BootstrapBlazorRoot>();
2621
cut.Contains("<div class=\"auto-generator\"></div>");
2722
}
2823

test/UnitTest/Components/ErrorLoggerTest.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using Microsoft.AspNetCore.Components.Web;
77
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.FileProviders;
9+
using Microsoft.Extensions.Hosting;
810

911
namespace UnitTest.Components;
1012

@@ -161,7 +163,7 @@ public void ErrorContent_Ok()
161163
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
162164
{
163165
pb.Add(a => a.EnableErrorLogger, true);
164-
pb.Add(a => a.ShowToast, false);
166+
pb.Add(a => a.ShowToast, true);
165167
pb.AddChildContent<Button>(pb =>
166168
{
167169
pb.Add(b => b.OnClick, () =>
@@ -217,6 +219,30 @@ public async Task TabItem_Error()
217219
((IDisposable)content).Dispose();
218220
}
219221

222+
[Fact]
223+
public async Task TabItem_Production_Error()
224+
{
225+
var context = new TestContext();
226+
context.JSInterop.Mode = JSRuntimeMode.Loose;
227+
context.Services.AddSingleton<IHostEnvironment, MockProductionEnironment>();
228+
context.Services.AddBootstrapBlazor();
229+
context.Services.GetRequiredService<ICacheManager>();
230+
231+
var cut = context.RenderComponent<BootstrapBlazorRoot>(pb =>
232+
{
233+
pb.AddChildContent<Tab>(pb =>
234+
{
235+
pb.AddChildContent<TabItem>(pb =>
236+
{
237+
pb.Add(a => a.Text, "Text1");
238+
pb.Add(a => a.ChildContent, builder => builder.AddContent(0, RenderButton()));
239+
});
240+
});
241+
});
242+
var button = cut.Find("button");
243+
await cut.InvokeAsync(() => button.Click());
244+
}
245+
220246
private RenderFragment RenderButton() => builder =>
221247
{
222248
builder.OpenComponent<Button>(0);
@@ -227,4 +253,15 @@ private RenderFragment RenderButton() => builder =>
227253
}));
228254
builder.CloseComponent();
229255
};
256+
257+
class MockProductionEnironment : IHostEnvironment
258+
{
259+
public string EnvironmentName { get; set; } = "Production";
260+
261+
public string ApplicationName { get; set; } = "Test";
262+
263+
public string ContentRootPath { get; set; } = "UniTest";
264+
265+
public IFileProvider ContentRootFileProvider { get; set; } = null!;
266+
}
230267
}

test/UnitTest/Core/TestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public TestBase()
1313
{
1414
Context = new TestContext();
1515
Context.JSInterop.Mode = JSRuntimeMode.Loose;
16+
17+
Context.Services.AddMockEnvironment();
1618
}
1719
}

test/UnitTest/Extensions/IServiceCollectionExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
55

66
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.FileProviders;
8+
using Microsoft.Extensions.Hosting;
79

810
namespace Microsoft.Extensions.DependencyInjection;
911

@@ -24,4 +26,21 @@ public static IServiceCollection AddConfiguration(this IServiceCollection servic
2426
services.AddSingleton<IConfiguration>(config);
2527
return services;
2628
}
29+
30+
public static IServiceCollection AddMockEnvironment(this IServiceCollection services)
31+
{
32+
services.AddSingleton<IHostEnvironment, MockEnvironment>();
33+
return services;
34+
}
35+
36+
class MockEnvironment : IHostEnvironment
37+
{
38+
public string EnvironmentName { get; set; } = "Development";
39+
40+
public string ApplicationName { get; set; } = "Test";
41+
42+
public string ContentRootPath { get; set; } = "UnitTest";
43+
44+
public IFileProvider ContentRootFileProvider { get; set; } = null!;
45+
}
2746
}

test/UnitTest/Services/MaskServiceTest.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ namespace UnitTest.Services;
88
/// <summary>
99
/// MaskService 单元测试
1010
/// </summary>
11-
public class MaskServiceTest : TestBase
11+
public class MaskServiceTest : BootstrapBlazorTestBase
1212
{
1313
[Fact]
1414
public async Task Mask_Ok()
1515
{
16-
var context = new TestContext();
17-
context.JSInterop.Mode = JSRuntimeMode.Loose;
18-
context.Services.AddBootstrapBlazor();
19-
20-
var maskService = context.Services.GetRequiredService<MaskService>();
21-
var cut = context.RenderComponent<BootstrapBlazorRoot>(pb =>
16+
var maskService = Context.Services.GetRequiredService<MaskService>();
17+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
2218
{
2319
pb.AddChildContent<Button>(pb =>
2420
{
@@ -49,12 +45,8 @@ await maskService.Show(new MaskOption()
4945
[Fact]
5046
public async Task Container_Ok()
5147
{
52-
var context = new TestContext();
53-
context.JSInterop.Mode = JSRuntimeMode.Loose;
54-
context.Services.AddBootstrapBlazor();
55-
56-
var maskService = context.Services.GetRequiredService<MaskService>();
57-
var cut = context.RenderComponent<BootstrapBlazorRoot>(pb =>
48+
var maskService = Context.Services.GetRequiredService<MaskService>();
49+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
5850
{
5951
pb.AddChildContent<Button>(pb =>
6052
{
@@ -87,12 +79,8 @@ await maskService.Show(new MaskOption()
8779
[Fact]
8880
public async Task Show_Component()
8981
{
90-
var context = new TestContext();
91-
context.JSInterop.Mode = JSRuntimeMode.Loose;
92-
context.Services.AddBootstrapBlazor();
93-
94-
var maskService = context.Services.GetRequiredService<MaskService>();
95-
var cut = context.RenderComponent<BootstrapBlazorRoot>(pb =>
82+
var maskService = Context.Services.GetRequiredService<MaskService>();
83+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
9684
{
9785
pb.AddChildContent<Button>(pb =>
9886
{
@@ -109,12 +97,8 @@ public async Task Show_Component()
10997
[Fact]
11098
public async Task Show_Type()
11199
{
112-
var context = new TestContext();
113-
context.JSInterop.Mode = JSRuntimeMode.Loose;
114-
context.Services.AddBootstrapBlazor();
115-
116-
var maskService = context.Services.GetRequiredService<MaskService>();
117-
var cut = context.RenderComponent<BootstrapBlazorRoot>(pb =>
100+
var maskService = Context.Services.GetRequiredService<MaskService>();
101+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
118102
{
119103
pb.AddChildContent<Button>(pb =>
120104
{

0 commit comments

Comments
 (0)