-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace UnitTest.Core; | ||
|
||
[Collection("BlazorButtonTestContext")] | ||
public class BootstrapBlazorButtonTestBase | ||
{ | ||
protected TestContext Context { get; } | ||
|
||
protected ICacheManager Cache { get; } | ||
|
||
public BootstrapBlazorButtonTestBase() | ||
{ | ||
Context = BootstrapBlazorButtonTestHost.Instance; | ||
Cache = BootstrapBlazorButtonTestHost.Cache; | ||
} | ||
} | ||
|
||
[CollectionDefinition("BlazorButtonTestContext")] | ||
public class BootstrapBlazorButtonTestCollection : ICollectionFixture<BootstrapBlazorButtonTestHost> | ||
{ | ||
|
||
} | ||
|
||
public class BootstrapBlazorButtonTestHost : IDisposable | ||
{ | ||
[NotNull] | ||
internal static TestContext? Instance { get; private set; } | ||
|
||
[NotNull] | ||
internal static ICacheManager? Cache { get; private set; } | ||
|
||
public BootstrapBlazorButtonTestHost() | ||
{ | ||
Instance = new TestContext(); | ||
|
||
// Mock 脚本 | ||
Instance.JSInterop.Mode = JSRuntimeMode.Loose; | ||
|
||
ConfigureServices(Instance.Services); | ||
|
||
ConfigureConfiguration(Instance.Services); | ||
|
||
// 渲染 BootstrapBlazorRoot 组件 激活 ICacheManager 接口 | ||
Cache = Instance.Services.GetRequiredService<ICacheManager>(); | ||
} | ||
|
||
protected virtual void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddBootstrapBlazor(); | ||
services.ConfigureJsonLocalizationOptions(op => | ||
{ | ||
op.IgnoreLocalizerMissing = false; | ||
}); | ||
services.AddSingleton<ILookupService, FooLookupService>(); | ||
} | ||
|
||
protected virtual void ConfigureConfiguration(IServiceCollection services) | ||
{ | ||
// 增加单元测试 appsettings.json 配置文件 | ||
services.AddConfiguration(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Instance.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
class FooLookupService : LookupServiceBase | ||
{ | ||
public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data) | ||
{ | ||
IEnumerable<SelectedItem>? ret = null; | ||
|
||
if (key == "FooLookup") | ||
{ | ||
ret = new SelectedItem[] | ||
{ | ||
new("v1", "LookupService-Test-1"), | ||
new("v2", "LookupService-Test-2") | ||
}; | ||
} | ||
return ret; | ||
} | ||
} | ||
} |