Reqnroll BDD测试在Blazor MAUI Hybrid应用中运行失败求助
问题描述
使用VS 2022创建新的Maui Blazor应用,安装Reqnroll插件并在解决方案内创建测试项目,按文档要求添加对Maui Blazor应用的项目引用。将两个项目的TargetFrameworks修改为net8.0-windows10.0.19041.0后,构建项目及dummy test可正常运行,应用也能正常启动。但在终端执行dotnet test时出现错误:类型初始化器引发异常,根源为WinRT.ActivationFactory1`的类型初始化失败,COM类未注册(错误码0x80040154)。
Maui Blazor项目文件
<Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks> <OutputType>Exe</OutputType> <RootNamespace>TestForReqnroll</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <EnableDefaultCssItems>false</EnableDefaultCssItems> <Nullable>enable</Nullable> <!-- Display name --> <ApplicationTitle>TestForReqnroll</ApplicationTitle> <!-- App Identifier --> <ApplicationId>com.companyname.testforreqnroll</ApplicationId> <!-- Versions --> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationVersion>1</ApplicationVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> <Platforms>x64</Platforms> </PropertyGroup> <ItemGroup> <!-- App Icon --> <MauiIcon Update="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> <!-- Splash Screen --> <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> <!-- Images --> <MauiImage Include="Resources\Images\*" /> <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" /> <!-- Custom Fonts --> <MauiFont Include="Resources\Fonts\*" /> <!-- Raw Assets (also remove the "Resources\Raw" prefix) --> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" /> </ItemGroup> </Project>
测试项目文件
<Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFrameworks>net8.0-windows10.0.19041.0</TargetFrameworks> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <Platforms>x64</Platforms> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> <PackageReference Include="Reqnroll.NUnit" Version="1.0.0" /> <PackageReference Include="nunit" Version="3.14.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> <PackageReference Include="FluentAssertions" Version="6.12.0" /> </ItemGroup> <ItemGroup> <Folder Include="Drivers\" /> <Folder Include="Support\" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\TestForReqnroll\TestForReqnroll.csproj" /> </ItemGroup> </Project>
测试步骤定义
using NUnit.Framework; using TestForReqnroll.Components; namespace TestForReqnroll.test.StepDefinitions { [Binding] public class TestStepDefinitions { private testClass testClass; [Given("The class has been instantiated")] public void GivenTheClassHasBeenInstantiated() { testClass = new testClass(); } [When("The function is called")] public void WhenTheFunctionIsCalled() { testClass.testFunction(); } [Then("it should return true")] public void ThenItShouldReturnTrue() { Assert.AreEqual(true, true); } } }
解决方案截图

解决方案
这个错误的核心原因是dotnet test默认非交互模式无法正确初始化MAUI依赖的WinRT/COM组件,可尝试以下方法解决:
管理员权限运行终端
右键终端选择「以管理员身份运行」,再执行dotnet test,部分COM组件注册需要管理员权限。指定测试运行平台
在命令中明确指定Windows x64平台:dotnet test -r win-x64调整测试项目配置
在测试项目的.csproj中添加以下配置,确保测试以Windows桌面模式运行,并修改输出类型:<PropertyGroup> <OutputType>Library</OutputType> <UseWindowsForms>true</UseWindowsForms> <UseWPF>true</UseWPF> <IsPackable>false</IsPackable> </PropertyGroup>修复MAUI安装
若上述方法无效,可能是MAUI组件未正确注册:- 打开VS Installer,选择「修改」VS 2022安装,确保「Mobile development with .NET」组件完整,点击「修改」重新安装;
- 或运行命令修复MAUI workload:
dotnet workload repair maui
临时使用VS内置测试工具
既然VS内部能正常运行测试,可优先使用VS测试资源管理器执行测试,避开终端环境限制。
内容的提问来源于stack exchange,提问作者ninjagast
相关产品推荐
相关产品推荐

