c# - 有没有办法在bunit测试中设置OnAfterRender方法的firstRender-Variable?

标签 c# xunit bunit

假设我有以下类,我想同时测试 firstRender = falsefirstRender=true “路径”。为此,我使用了 bunit 和 xunit。

public class ToTest {
    protected async override void OnAfterRender(bool firstRender)
    {
         if(firstRender)
         {
              //do stuff
         }
         else
         {
              //do other stuff
         }
    }
}
public class TestClass : TestContext
{
    
    [Fact]
    public void Test()
    {
        //Arrange stuff before this method
        var cut = RenderComponent<News>(parameters => parameters
        .Add(p => p.authenticationStateTask, stateProvider.GetAuthenticationStateAsync()));
    }

}

有没有办法在bunit测试中设置OnAfterRender方法的firstRender-Variable? 我是否必须调整 RenderComponent 参数?

最佳答案

bUnit 允许您对被测组件执行多次渲染。第一个渲染将始终将 firstRender 设置为 true,任何其他渲染都将其设置为 false,就像常规 Blazor 运行时一样。

例如:

public class TestClass : TestContext
{
    
    [Fact]
    public void Test()
    {
        // First render, where firstRender is true
        var cut = RenderComponent<News>(parameters => parameters
           .Add(p => p.Param, "Bar"));

       // Second render, where firstRender is false
       cut.Render();

       // Third render, with new parameters set during the render life-cycle 
       // and where firstRender is also false
       cut.SetParametersAndRender(parameters => parameters
           .Add(p => p.Param, "Foo"));
    }

}

在这里了解更多:https://bunit.dev/docs/interaction/trigger-renders.html

关于c# - 有没有办法在bunit测试中设置OnAfterRender方法的firstRender-Variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67887198/

相关文章:

c# - Parallel.ForEach 中的多个异步等待链接

c# - 如何在 Windows Phone 8.1 中阻止一个特定的键

c# - 检查用 Split() 创建的数组是否为空 C#

c# - 使用 XUnit c# 进行功能测试中的类型化配置

c# - 在 Visual Studio 中针对多个配置运行单个测试

asp.net-core - Blazor 服务器页面单元测试 - 如何模拟 Blazor 服务器页面中使用的辅助 signalR 客户端连接

c# - Blazor 测试 InputDate 与 bUnit 的绑定(bind)

c# - 可空值还是默认值?

c# - xUnit和测试的多个数据记录