用bUnit和c#模拟或初始化EditForm和EditContext



我使用Radzen的Blazor组件,我有以下形式:

<EditForm @ref=@_editForm Model="@_selectedItem">
<div class="row">
<div class="col d-flex">
<RadzenDataGrid @ref=@_grid
IsLoading=@_isLoading
Count=@_totalItems
Data=@_items
...
TItem="UserItem">
<EmptyTemplate>
<p class="datagrid-no-records">No matching results</p>
</EmptyTemplate>
<Columns>
<RadzenDataGridColumn TItem="UserItem" Property="User.Email" Title="User Name" TextAlign="TextAlign.Left">
<Template Context="item">
<RadzenLabel Text=@item.User.Email MouseEnter="@item.OnEmailTooltipShow" MouseLeave="@item.OnTooltipHide"/>
</Template>
<EditTemplate Context="item">
<CascadingValue Value=@_editForm.EditContext IsFixed="false">
<div class="cell-item">
<RadzenTextBox @bind-Value="item.User.Email" class="w-100 mb-1" @oninput="@item.OnEmailChange" />
<ValidationMessage For="(() => item.User.Email)" />
</div>
</CascadingValue>
</EditTemplate>
</RadzenDataGridColumn>
...
</Columns>
</RadzenDataGrid>
</div>
</div>
</EditForm>

和后面的代码:

private List<UserItem> _items = new();
private RadzenDataGrid<UserItem> _grid;
private UserDto _selectedItem = new();
private EditForm _editForm;
protected override async Task OnAfterRenderAsync(bool firstRender)
{    
if (firstRender && _editForm?.EditContext != null)
{
_editForm.EditContext.OnFieldChanged -= EditContext_OnFieldChanged;
_editForm.EditContext.OnFieldChanged += EditContext_OnFieldChanged;     
}
}

我想写一个bUnit测试来覆盖:

if (firstRender && _editForm?.EditContext != null)
{
_editForm.EditContext.OnFieldChanged -= EditContext_OnFieldChanged;
_editForm.EditContext.OnFieldChanged += EditContext_OnFieldChanged;     
}

单元测试如下:

using var ctx = new TestContext();
...
var userManagementPage = ctx.RenderComponent<UserManagement>();

但该块从未到达,因为_editForm总是null,我不知道如何初始化模型和EditContext

怎么做?

//你可以试试

userManagementPage.Instance._editForm = new Telerik.Blazor.Components.TelerikForm
{
EditContext = new EditContext(new yourModel())
};

相关内容

  • 没有找到相关文章

最新更新