在没有参数的情况下将属性设置为Bunit

发布于 2025-01-29 11:55:10 字数 2220 浏览 2 评论 0原文

我有一个抽象的基类,我正在尝试测试具有参数的属性,用于@ref。我找不到将财产传递给我的Bunit渲染器的方法。

我使用此属性来存储我在整个基类中访问的信息。

public abstract class ColumnBase<TRowData> : ComponentBase
{
    [Parameter] public string? Title { get; set; }
    [Parameter] public RenderFragment<FunctionalColumn<TRowData>.CellInfo>? CellTemplate { get; set; }

    public FunctionalColumn<TRowData> ColumnReference { get; protected set; } = default!;
}

这个类通常会像这样使用。.在

styledcolumn.cs

@typeparam TRowData
@inherits ColumnBase<TRowData>

<FunctionalColumn
    @ref=ColumnReference
    Title=@Title
    CellTemplate=CellTemplate
    class="some styling" />

中,我正在尝试测试使用columnReference的某些功能,因此它不能为null。

首先,我尝试了这个。

columnbasetests.cs

public class ColumnBaseTests : TestContext
{
    [Fact]
    public void TestRenderFragment()
    {
        var cb = new ColumnBase<TestData>() { FunctionalColumn = new FunctionalColumn() };

        // then how to test render fragment?
        // wanted something like cb.CellTemplate.MarkupMatches("my markup")
    }
}

然后我尝试以Bunit中的实际标记方式进行操作。这是一个孩子)。但是,这也不起作用,因为没有[参数]列的属性。

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<ColumnBase<TestData>>(parameters => parameters
            .Add(p => p.FunctionalColumn , new FunctionalColumn <TestData>())
            //.Add(p => p.Title, "..")
        )
    );
}

最后,我尝试在一个实例中通过。 (请注意,我必须用functionColumn制作一个构造函数,只是为了查看它是否有效,而是,addChildContent使用了正确的构造函数(列的一个)首先,然后使用空白构造函数,覆盖它)。因此,当访问functionColumn时,我得到了无效的异常。

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<ColumnBase<TestData>>(p => new ColumnBase<TestData>(new FunctionalColumn()) { Title = ".."}
        )
    );
}

I have an abstract base class I am trying to test that has parameters and a property that is used for @ref. I can't find a way to pass in my property to my Bunit renderer.

I use this property for storing information that I access throughout the base class.

public abstract class ColumnBase<TRowData> : ComponentBase
{
    [Parameter] public string? Title { get; set; }
    [Parameter] public RenderFragment<FunctionalColumn<TRowData>.CellInfo>? CellTemplate { get; set; }

    public FunctionalColumn<TRowData> ColumnReference { get; protected set; } = default!;
}

This class would normally be used like so.. in

StyledColumn.cs

@typeparam TRowData
@inherits ColumnBase<TRowData>

<FunctionalColumn
    @ref=ColumnReference
    Title=@Title
    CellTemplate=CellTemplate
    class="some styling" />

I am trying to test some of the functionality that uses the ColumnReference so it can't be null..

First, I tried this.

ColumnBaseTests.cs

public class ColumnBaseTests : TestContext
{
    [Fact]
    public void TestRenderFragment()
    {
        var cb = new ColumnBase<TestData>() { FunctionalColumn = new FunctionalColumn() };

        // then how to test render fragment?
        // wanted something like cb.CellTemplate.MarkupMatches("my markup")
    }
}

Then I tried doing it in the actual markup way you do in Bunit.. (note, this column has a cascaded grid so it is a child). However this did not work either because there is no [Parameter] attribute for the column.

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<ColumnBase<TestData>>(parameters => parameters
            .Add(p => p.FunctionalColumn , new FunctionalColumn <TestData>())
            //.Add(p => p.Title, "..")
        )
    );
}

Finally, I tried just passing in an instance of it. (note, I had to make a constructor with the FunctionalColumn just to see if it would work, but instead, the AddChildContent used the correct constructor (the one with the column) first, then used the blank constructor one, overwriting it). So then I got a null exception when accessing the FunctionalColumn.

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<ColumnBase<TestData>>(p => new ColumnBase<TestData>(new FunctionalColumn()) { Title = ".."}
        )
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2025-02-05 11:55:10

我最终要使用的解决方案是仅测试RenderFragement

var cellTemplateRenderFragment = Render(s => s.AddContent(1, new ColumnBase<TestData>(new FunctionalColumn()).CellTemplate, 
    new FunctionalColumn<TestData>.CellInfo {
        // some variable info to pass into the render fragement
    }));
cellTemplateRenderFragment .MarkupMatches("my markup");

另外,您可以创建一个可测试的类,以设置基础属性。

private class TestableColumnBase<T> : ColumnBase<T>
{
    [Parameter] public FunctionalColumn<T> FunctionalColumnParam {
        get { return base.FunctionalColumn; }
        set { base.FunctionalColumn = value; }
    }
    
}

然后可以像

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<TestableColumnBase<TestData>>(parameters => parameters
            .Add(p => p.FunctionalColumnParam , new FunctionalColumn<TestData>())
            //.Add(p => p.Title, "..")
        )
    );
}

The solution I ended up going with was to just test the RenderFragement

var cellTemplateRenderFragment = Render(s => s.AddContent(1, new ColumnBase<TestData>(new FunctionalColumn()).CellTemplate, 
    new FunctionalColumn<TestData>.CellInfo {
        // some variable info to pass into the render fragement
    }));
cellTemplateRenderFragment .MarkupMatches("my markup");

Alternatively you could create a testable class that set the underlying property.

private class TestableColumnBase<T> : ColumnBase<T>
{
    [Parameter] public FunctionalColumn<T> FunctionalColumnParam {
        get { return base.FunctionalColumn; }
        set { base.FunctionalColumn = value; }
    }
    
}

And then can be used like

[Fact]
public void CellTemplateIsSet()
{
    var grid = RenderComponent<Grid<TestData>>(parameterBuilder => parameterBuilder
        .AddChildContent<TestableColumnBase<TestData>>(parameters => parameters
            .Add(p => p.FunctionalColumnParam , new FunctionalColumn<TestData>())
            //.Add(p => p.Title, "..")
        )
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文