From 807f08f29e616c9036f7a2f4b522c152fd00e507 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Fri, 8 Apr 2022 14:41:14 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Speech=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/SpeetchTest.cs | 32 ++++++++++ test/UnitTest/Core/SpeechTestBase.cs | 85 +++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 test/UnitTest/Components/SpeetchTest.cs create mode 100644 test/UnitTest/Core/SpeechTestBase.cs diff --git a/test/UnitTest/Components/SpeetchTest.cs b/test/UnitTest/Components/SpeetchTest.cs new file mode 100644 index 000000000..b01a5a933 --- /dev/null +++ b/test/UnitTest/Components/SpeetchTest.cs @@ -0,0 +1,32 @@ +// Copyright (c) Argo Zhang (argo@163.com). 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.DependencyInjection; + +namespace UnitTest.Components; + + +public class SpeetchTest : SpeechTestBase +{ + [Fact] + public async Task Speech_Ok() + { + var result = ""; + var speechService = Context.Services.GetRequiredService(); + var cut = Context.RenderComponent(); + await speechService.InvokeAsync(new SpeechOption() + { + MethodName = "Test", + TargetLanguage = "zh-CN", + SpeechRecognitionLanguage = "zh-CN", + Callback = new Func(v => + { + result = v; + return Task.CompletedTask; + }) + }); + + Assert.Equal("MockSpeechProvider", result); + } +} diff --git a/test/UnitTest/Core/SpeechTestBase.cs b/test/UnitTest/Core/SpeechTestBase.cs new file mode 100644 index 000000000..3b5b76a82 --- /dev/null +++ b/test/UnitTest/Core/SpeechTestBase.cs @@ -0,0 +1,85 @@ +// Copyright (c) Argo Zhang (argo@163.com). 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; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace UnitTest.Core; + +[Collection("SpeechTestContext")] +public class SpeechTestBase +{ + protected TestContext Context { get; } + + public SpeechTestBase() + { + Context = SpeechTestHost.Instance; + } +} + +[CollectionDefinition("SpeechTestContext")] +public class SpeechTestCollection : ICollectionFixture +{ + +} + +public class SpeechTestHost : IDisposable +{ + [NotNull] + internal static TestContext? Instance { get; private set; } + + public SpeechTestHost() + { + Instance = new TestContext(); + + // Mock 脚本 + Instance.JSInterop.Mode = JSRuntimeMode.Loose; + + ConfigureServices(Instance.Services); + + ConfigureConfigration(Instance.Services); + + // 渲染 SpeechRoot 组件 激活 ICacheManager 接口 + Instance.Services.GetRequiredService(); + } + + protected virtual void ConfigureServices(IServiceCollection services) + { + services.AddBootstrapBlazor(); + services.TryAddScoped(); + services.TryAddScoped(); + services.ConfigureIPLocatorOption(options => + { + options.LocatorFactory = provider => new BaiDuIPLocator(); + }); + services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { typeof(Alert).Assembly }); + } + + protected virtual void ConfigureConfigration(IServiceCollection services) + { + // 增加单元测试 appsettings.json 配置文件 + services.AddConfiguration(); + } + + public void Dispose() + { + Instance.Dispose(); + GC.SuppressFinalize(this); + } + + class MockSpeechProvider : ISpeechProvider + { + public async Task InvokeAsync(SpeechOption option) + { + var method = option.MethodName; + var language = option.TargetLanguage; + var recognitionLanguage = option.SpeechRecognitionLanguage; + if (option.Callback != null) + { + await option.Callback("MockSpeechProvider"); + } + } + } +} -- Gitee