Hello,
I have an issue with Unit tests that used to work.
I’m using bUnit to test the Blazor Autocomplete; before the (latest (not sure though)) update the tests passed.
The tests do the following:
1. bUnit renders the autocomplete
2. I use bUnit to fill in 2 chars in the autocomplete
3. The OnRead for the Autocomplete should get triggered
4. The autocomplete should than call a service method, which I mocked (using Moq)
5. I than assert the Mocked service method to be hit at least once
This used to work like a charm, but now it seems that the OnRead doesn’t get hit.
Were there any changes in when the OnRead Event gets triggered for the Autocomplete?
I decided to create a post here instead of a bUnit github issue; since the bUnit version remained the same (since the working version).
Here is some example code:
a. Component code behind:
public async Task OnRead(AutoCompleteReadEventArgs args)
{
Telerik.DataSource.FilterDescriptor filter = args.Request.Filters[0] as Telerik.DataSource.FilterDescriptor;
if (args.Request.Filters.Count < 0)
return;
string userInput = filter.Value.ToString();
if (string.IsNullOrWhiteSpace(userInput))
{
await SetToZero(); //defaults some values
return;
}
await CallService(userInput);
}
b. Component markup: <TelerikAutoComplete Data="@_data "
ClearButton="true" OnRead="@OnRead" OnChange="@OnChange"
FilterOperator="Telerik.Blazor.StringFilterOperator.Contains"
Filterable="true"
MinLength="2" //OnRead only gets hit when min 2 chars are provided
Placeholder="@PlaceHolderValue" @bind-Value="@Value"/>
c. bUnit test
Mock<InterfaceToMock> mock = new Mock<InterfaceToMock>();
mock.Setup(c => c.GetSomethingByInput(It.IsAny<string>()))
.Returns(Task.FromResult(new List<Something>()));
ctx.Services.AddScoped(sp =>
{
return mock.Object;
});
var rootComponentMock = new Mock<TelerikRootComponent>();
var cut = ctx.RenderComponent<TheComponentContainingtheAutocomplete>
(rc => rc.AddCascadingValue(rootComponentMock.Object));
cut.Find("input").Input("t");
cut.WaitForAssertion(() =>
{
//This passes
countryAutocompleteMock.Verify(m => m.GetCountriesByName(It.IsAny<string>()), Times.Never());
});
cut.Find("input").Input("tt");
cut.WaitForAssertion(() =>
{
//This fails, while it should’ve been called by the OnRead
countryAutocompleteMock.Verify(m => m.GetCountriesByName(It.IsAny<string>()), Times.Once());
});
Any idea why the OnRead doesn’t get called when 2 char input is provided in the AutoComplete within the test?
It does get called when debugging the component.
Thanks in advance!