rodrigobastosv / loading_overlay

MIT License
122 stars 32 forks source link

How to initialize the package on unit testing? #31

Closed luis-cruzt closed 2 years ago

luis-cruzt commented 2 years ago

Is there a way to initialize the package without using the Widget?

I'm getting this error on my unit test:

LateInitializationError: Field '_overlayController@1100390500' has not been initialized

rodrigobastosv commented 2 years ago

Hi @luis-cruzt ,

Thanks a lot for using this package.

No. You have to use the widget. All the initialization is done when the widget is inserted on the widget tree.

But you can just wrap on your test widget and it will just work!

luis-cruzt commented 2 years ago

so i can't use this package on unit testing?

rodrigobastosv commented 2 years ago

Sure you can :)

But you have to wrap the widget under testing in the LoaderOverlay.

Something like that:


await tester.pumpWidget(
   LoaderOverlay(
      child: YourWidgetUnderTesting(),
   ),
);

Sorry for the poor typing, i'm on the phone.

luis-cruzt commented 2 years ago

As far as i know pumpWidget belongs to widget testing, and i'm trying to do unit testing: https://docs.flutter.dev/testing

rodrigobastosv commented 2 years ago

I thought you were doing Widget testing. My bad.

What are you testing then? OverlayController is a widget and should not afect in any manner unit tests.

Can you please provide more context?

luis-cruzt commented 2 years ago

I'm trying to do unit testing, i have this function:

/// Log in the user
  Future login(BuildContext context) async {    
    context.loaderOverlay.show();
    (await state.loginRepository.signIn(
      email: state.userEmail,
      password: state.userPassword,
    ))
        .fold(
      (l) {        
        context.loaderOverlay.hide();
      },
      (r) {
        // Navigate to login screen
        context.loaderOverlay.hide();
      },
    );
  }

Then, the test fails on the context.loaderOverlay.show() line with the following error:

LateInitializationError: Field '_overlayController@1100390500' has not been initialized

That's why i'm asking if there's a way to initialize the overlay controller without providing the LoaderOverlay widget at the top of the app.

Thanks!

rodrigobastosv commented 2 years ago

Thanks for the information. In this case you cannot initialize the overlay without a widget. This what you doing is not a good practice IMO. Showing/Hiding loading, navigating are purely UI stuff and because of that belongs to the Widget layer.

In the same way you could not test navigation, because it also depends on the widget tree. I highly recomend you to refactor your code. Trust me, it will be a lot more testable.

If you need more help, please let me know.

luis-cruzt commented 2 years ago

Well IMO I don't think it's a bad practice because I'm using only the BuildContext, but thank you for the response!

So the answer is, no, we can't use this package for unit testing :)

rodrigobastosv commented 2 years ago

I have a lot of projects that use OverlayLoader and have unit tests :). In those projects i try not to mix ui logic with business logic.

But everyone knows what is best for your projects. I want to thank you again for using this package!

I'll close this issue for now, but feel free to reopen if needes