在没有参数的情况下将属性设置为Bunit
我有一个抽象的基类,我正在尝试测试具有参数
的属性,用于@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终要使用的解决方案是仅测试
RenderFragement
另外,您可以创建一个可测试的类,以设置基础属性。
然后可以像
The solution I ended up going with was to just test the
RenderFragement
Alternatively you could create a testable class that set the underlying property.
And then can be used like