2

想象一下,我有以下课程,我想同时测试 thefirstRender = 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-Parameters 吗?

4

1 回答 1

1

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

于 2021-06-08T13:48:33.530 回答