Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a Delayed Future in Widget test stops the test from completing #19

Closed
maks opened this issue Mar 2, 2021 · 5 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@maks
Copy link

maks commented Mar 2, 2021

@felangel thanks for this fantastic package, you've saved mocking for us in the NS age!

I've have run into an issue though and I'm not sure if this is a case of my not doing something correctly but I've found that trying to use Future.delayed() in a mocked method in a widgetTest causes the test to hang and never complete.

My code is:

testWidgets('home page shows progress indicator when loading',
      (tester) async {
    final api = MockApiService();
    final lvm = LoginViewModel(api);

    when(api).calls(#requestLoginCode).thenAnswer((_) async {
      await Future<void>.delayed(Duration(seconds: 1));
      return APIResult.Error;
    });

    final w = ProviderScope(
      overrides: [loginProvider.overrideWithProvider(Provider((ref) => lvm))],
      child: HomePage(),
    );

    await tester.pumpWidget(wrapForTest(w));

    await lvm.requestCode('12345');

    await tester.pump();

    await tester.pump(Duration(seconds: 2));

    final hasSpinner = tester.any(find.byType(CircularProgressIndicator));
  
    expect(hasSpinner, true);
  });

where requestCode() calls the ApiService objects requestLoginCode() method which returns a Future that completes once its network API call (in the real, nnon mocked ApiService class) completes.

If I remove the Future.delayed() everything works as expected, BUT I want the delay in there because I'm trying to test that a CircularProgressIndicator is displayed during the period before the Future returned by requestLoginCode completes.

I've included the Riverpod setup code, but I don't think its relevant here.

My suspicion is this is maybe due to widgetTests running inside a FakeAsync zone though according to this SO question this approach should work (or at least used to work with mockito).

@felangel felangel self-assigned this Mar 2, 2021
@felangel felangel added the investigating Investigating a reported issue label Mar 5, 2021
@felangel
Copy link
Owner

felangel commented Mar 6, 2021

Hi @maks 👋
Thanks for opening an issue!

Are you still able to reproduce this in the latest release (v0.0.2-dev.5)? Thanks! 🙏

@felangel felangel added question Further information is requested waiting for response Waiting for follow up and removed investigating Investigating a reported issue labels Mar 6, 2021
@felangel
Copy link
Owner

felangel commented Mar 8, 2021

@maks any updates or can this be closed?

@maks
Copy link
Author

maks commented Mar 8, 2021

@felangel sorry for the slow reply, its been a long weekend holiday here.
I've upgraded to 0.0.2-dev.5 and changed my test to now use new type-safe syntax:

    when(() => authRepo.requestOtp('12345')).thenAnswer((_) async {
      await Future<void>.delayed(Duration(seconds: 1));
      return AuthResult.error(400, 'test error');
    });

But unfortunately still seeing the test hang.

@felangel
Copy link
Owner

felangel commented Mar 8, 2021

@maks no worries thanks for getting back to me!

Just to clarify, you said this was not a problem using mockito?

I think this is expected because in a widget test you will need to use tester.runAsync(() {...}) in this situation.

@maks
Copy link
Author

maks commented Mar 8, 2021

@felangel actually I realised when I looked again at my test that I was making it more complicated than it needed to be as I could just override my providers state and not even need to worry about the async behaviour.

I still think there maybe an issue with async handling as I would have expected that FakeAsync to work with the Future I was declaring inside the FaskAsync Zone, but I'll close this for now until I get a chance to see if I can write a simple standalone test to reproduce the issue. Thanks again for looking at it!

@maks maks closed this as completed Mar 8, 2021
@felangel felangel removed the waiting for response Waiting for follow up label Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants