1

I have a conditional mock data with moq like this where it returns different data based on if the parameter is null or not.

 locationMock
    .Setup(x => x.GetAllLocations(It.IsAny<SearchRequest>()))
    .Returns((SearchRequest req) =>
        {
            return req.SearchText == null
                ? thread
                : filteredThread;
        });

I register the service like this Services.AddSingleton(locationMock.Object);

I then have an input box where I input "TEST LOCATION" and click search icon which should filter the data AKA return the "filteredThread". When I debug through it, I can see it hit the different conditions. However, the data that is returned is always "thread" which does not contain the filtered data but the original data.

I'm wondering what I'm doing wrong or if there is something I can do to achieve the conditional return of data showing up in bunit.

var cut = RenderComponent<LocationList>();
var instance = cut.Instance;
instance.use = UseMode.None;
instance.Locations = locationData;

cut.Render();

cut.Find(".prompt.input-box").Input("TEST LOCATION");            
cut.Find(".search-icon").Click();

cut.Render();
var bob = cut.FindAll("table > tbody > tr");
cut.WaitForState(() => cut.FindAll("table > tbody > tr").Count == 1, TimeSpan.FromSeconds(15));
1
  • 1
    Setting parameter/properties directly on the instance component might not be a good idea. Instead try to use SetParametersAndRender(...). That will set the new parameters like they would have been from the normal Blazor runtime. More here: bunit.dev/docs/interaction/… Jun 7, 2021 at 14:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.