You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用bUnit测试Blazor的MudSelect组件?

解决bUnit测试MudSelect时列表项不渲染的问题

针对你遇到的MudSelect测试中列表项不渲染的问题,核心原因是下拉菜单的动态渲染时机未正确等待,以及部分JSInterop模拟缺失导致组件行为异常。以下是具体的修复方案:

问题分析

  1. MudSelect的下拉弹窗是点击后动态生成的,直接在点击后立刻查找列表项,bUnit还未完成组件更新渲染。
  2. 测试中缺少mudPopover.open的JS模拟,MudBlazor打开下拉时会调用该方法,未模拟会导致弹窗无法正常触发渲染。
  3. 提前获取menu元素的时机不对,点击前弹窗可能仅存在空容器,没有实际列表内容。

修复后的测试代码

using FluentAssertions;
using MudBlazor;
using MudBlazor.Services;

namespace App.UnitTests.Example;

public class ExampleTests : TestContext
{
    public ExampleTests()
    {
        Services.AddMudServices();
        // 补充mudPopover.open的模拟,这是打开下拉的关键JS调用
        JSInterop.SetupVoid("mudPopover.open", _ => true);
        JSInterop.SetupVoid("mudPopover.initialize", _ => true);
        JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true);
        JSInterop.Setup<int>("mudpopoverHelper.countProviders");
        JSInterop.SetupVoid("mudKeyInterceptor.updatekey", _ => true);
        // 可选:模拟mudPopover.close,避免后续可能的错误
        JSInterop.SetupVoid("mudPopover.close", _ => true);
    }
    
    [Fact]
    public void TestMudSelect()
    {
        var cut = RenderComponent<Wrapper>();
        
        var select = cut.FindComponent<MudSelect<int?>>();
        var input = cut.Find("div.mud-input-control");

        // 初始值验证
        select.Instance.Value.Should().BeNull();
        
        // 点击输入框触发下拉
        input.Click();
        
        // 等待弹窗打开并渲染完成,这里用WaitForAssertion确保列表项存在
        cut.WaitForAssertion(() => 
        {
            var listItems = cut.FindAll("div.mud-list-item");
            listItems.Count.Should().BeGreaterThan(0);
        });
        
        // 获取列表项并点击第二项
        var items = cut.FindAll("div.mud-list-item").ToArray();
        items[1].Click();
        
        // 验证选中值是否正确
        select.Instance.Value.Should().Be(1);
    }
}

关键修改点说明

  • 补充mudPopover.open模拟:MudSelect打开下拉时会调用该JS方法,缺少模拟会导致组件无法正确触发弹窗渲染逻辑。
  • 延迟查找列表项:用WaitForAssertion等待组件完成渲染更新,确保列表项已经生成后再进行操作,避免因渲染时机问题找不到元素。
  • 移除提前获取menu的代码:弹窗容器在点击前可能仅存在空结构,没有实际内容,点击后再通过列表项的选择器验证更可靠。

额外注意事项

  • 如果仍存在问题,可以尝试在点击后调用cut.Render()强制触发组件更新,但WaitForAssertion通常已经足够处理异步渲染。
  • 确保MudBlazor和bUnit的版本兼容,版本不匹配也可能导致组件测试行为异常。

内容的提问来源于stack exchange,提问作者baynezy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 22:30:59