BlazorTable
BlazorTable copied to clipboard
Unit test needs Logger
Describe the bug I am writing a unit test for a component but I get an exception' I realize that I am using bUnit to render my components but since the exception is because BlazorTable cannot be constructed because of a missing Logger dependency I was wondering if you might have a workaround?
To Reproduce Please create a standalone Page which reproduces the issue and paste the code here in a code block. More Examples
@using BlazorTable
<h2>Projects</h2>
<Table TableItem="BreakpointProjectSummary" DataLoader="_loader" Items="data" PageSize="5" SelectionType="selectionType" RowClickAction="RowClick" SelectedItems="selectedItems" ShowSearchBar="false">
<Column TableItem="BreakpointProjectSummary" Title="Project Id" Field="@(x => x.ProjectId)" Sortable="true" Width="10%" DefaultSortColumn="true" />
<Column TableItem="BreakpointProjectSummary" Title="Project Name" Field="@(x => x.ProjectName)" Sortable="true" Width="10%" DefaultSortColumn="true" />
<Column TableItem="BreakpointProjectSummary" Title="Company Name" Field="@(x => x.CompanyName)" Sortable="true" Width="10%" DefaultSortColumn="true" />
<Column TableItem="BreakpointProjectSummary" Title="Clent Type" Field="@(x => x.ClientType)" Sortable="true" Width="10%" DefaultSortColumn="true" />
<Pager ShowPageNumber="true" ShowTotalCount="true" />
</Table>
public partial class ProjectPicker
{
[Inject]
private IBreakpointManagementDataService dataService { get; set; }
private IDataLoader<BreakpointProjectSummary> _loader;
private IEnumerable<BreakpointProjectSummary> data;
private BreakpointProjectSummary selected;
private SelectionType selectionType = SelectionType.Single;
private List<BreakpointProjectSummary> selectedItems = new List<BreakpointProjectSummary>();
protected override async Task OnInitializedAsync()
{
_loader = new ProjectDataLoader(dataService);
data = (await _loader.LoadDataAsync(null)).Records;
}
public void RowClick(BreakpointProjectSummary data)
{
selected = data;
StateHasChanged();
}
}
public class ProjectDataLoader : IDataLoader<BreakpointProjectSummary>
{
private readonly IBreakpointManagementDataService _dataService;
public ProjectDataLoader(IBreakpointManagementDataService dataService)
{
_dataService = dataService;
}
public async Task<PaginationResult<BreakpointProjectSummary>> LoadDataAsync(FilterData parameters)
{
if (parameters == null) return new PaginationResult<BreakpointProjectSummary>();
IList<BreakpointProjectSummary> results;
if (parameters == null)
{
results = await _dataService.GetBreakpointProjects();
}
else if (parameters.Top == null)
{
results = await _dataService.GetBreakpointProjects();
}
else if (string.IsNullOrWhiteSpace(parameters.OrderBy))
{
results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value);
}
else
{
var order = parameters.OrderBy.Split(" ");
if (order.Length >= 2)
{
results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value, order[0]);
}
else
{
results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value);
}
}
var count = await _dataService.GetBreakpointProjectCount();
return new PaginationResult<BreakpointProjectSummary>
{
Records = results,
Skip = parameters?.Skip ?? 0,
Total = int.Parse(count),
Top = parameters?.Top ?? 0
};
}
}
Expected behavior I would expect the unit test to pass. But at a bare minimum not through an exception when bUnit renders my component.
Hey,
Can you give this a try? https://bunit.egilhansen.com/api/Bunit.Extensions.LoggerHelperExtensions.html If you get stuck, let me know where the code is and I can have a look.
Thank you!
So this details a CreateLogger method but I am not sure how to tie this into my component which uses BlazorTable?
On Tue, Mar 9, 2021 at 11:09 AM Ivan Josipovic [email protected] wrote:
Hey,
Can you give this a try? https://bunit.egilhansen.com/api/Bunit.Extensions.LoggerHelperExtensions.html If you get stuck, let me know where the code is and I can have a look.
Thank you!
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IvanJosipovic/BlazorTable/issues/273#issuecomment-794175692, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAUFM3SGELS2UZXIGUZM3ATTCZB4LANCNFSM4Y4AU3RA .