This is a migrated thread and some comments may be shown as answers.

probem unittesting with bunit

1 Answer 1498 Views
AutoComplete
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Veteran
Iron
Stefan asked on 20 Apr 2021, 11:58 AM

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!

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 20 Apr 2021, 02:31 PM

Hi Stefan,

I am not completely sure a unit test can cover this, because it requires an interaction with the component and that's typically a task for e2e tests. Moreover, bUnit cannot actually run JS Interop, so complex component interactions that may require it will not work (you can see more about this in the readme of this sample).

 

Regards,
Marin Bratanov
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Tags
AutoComplete
Asked by
Stefan
Top achievements
Rank 1
Veteran
Iron
Answers by
Marin Bratanov
Telerik team
Share this question
or