I am receiving this error to do a unit test with spec test and bunit for a project in Blazor and Blazorise:
Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer
Thi is the code of unit test:
using BlazorApp1.Pages;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Blazorise.Modules;
using Bunit;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
namespace TestProject1
{
[Binding]
public sealed class Feature1StepDefinitions
{
private IRenderedComponent<Home> _cut;
private readonly TestContext _ctx;
private readonly Mock<IClassProvider> _classProviderMock;
public Feature1StepDefinitions()
{
_ctx = new TestContext();
_ctx.Services.AddBlazorise(options =>
{
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
_ctx.Services.AddSingleton<IJSUtilitiesModule, JSUtilitiesModule>();
}
[Given("a list of stores")]
public void GivenTheComponentIsRendered()
{
}
[When("the page is rendered")]
public void GivenTheComponentIsRendered2()
{
/////In this line i receive the error!!!!!!!!!!!!
_cut = _ctx.RenderComponent<Home>();
}
[Then("the stores are loaded")]
public void ThenTheOnInitializedMethodShouldBeCalled()
{
//_cut.Instance.Stores.Should().NotBeNull();
//_cut.Instance.Stores.Should().HaveCount(2);
}
}
}
This is the file Home.razor
@page "/"
@using Blazorise.DataGrid
@using BlazorApp1.Models
@using Blazorise.Components
<DataGrid TItem="StoresDatagrid" Data="@stores">
<DataGridColumns>
<DataGridColumn TItem="StoresDatagrid" Field="@nameof(StoresDatagrid.StoreCode)" />
<DataGridColumn TItem="StoresDatagrid" Field="@nameof(StoresDatagrid.Name)" />
<DataGridColumn TItem="StoresDatagrid" Field="@nameof(StoresDatagrid.IsEnabled)" />
</DataGridColumns>
</DataGrid>
THis is the file Home.razor.cs
using BlazorApp1.Models;
using Microsoft.AspNetCore.Components;
namespace BlazorApp1.Pages
{
public partial class Home : ComponentBase
{
protected string test = "";
protected List<StoresDatagrid> stores;
protected override async Task OnInitializedAsync()
{
test = "ruben";
stores = GetStores();
}
private List<StoresDatagrid> GetStores()
{
return new List<StoresDatagrid>()
{
new StoresDatagrid
{
StoreCode = 40055,
Name = "Store 1",
IsEnabled = true
},
new StoresDatagrid
{
StoreCode = 40056,
Name = "Store 2",
IsEnabled = false
}
};
}
}
}
Could you please help me?