-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
feat: Test support for BlocObservers #4203
Comments
Hi @Rodsevich 👋 BlocObservers should not be overridden in blocTests. Can you please provide a link to a minimal reproduction sample that demonstrates the issue you're facing and I'm happy to take a closer look, thanks! |
Sure! |
@Rodsevich I'm still not sure I understand the problem. Can you please provide more context regarding what the specific issue is and ideally provide a minimal reproduction sample? It's not clear what the problem is based on the code you linked to, thanks! |
Sorry, @felangel. I misunderstood your request. I would like to achieve a way of including BlocObserver's behavior in tests, specially when they fail. ATM that's not possible without hard workarounds like the link provided before. class FailingObserver extends BlocObserver {
@override
void onEvent(Bloc<dynamic, dynamic> bloc, Object? event) {
super.onEvent(bloc, event);
throw StateError('catch me');
}
}
blocTest<CounterBloc, int>(
'should fail on event addition when FailingObserver provided',
build: () => CounterBloc(),
act: (bloc) => bloc.add(CounterEvent.increment),
errors: () => [isA<StateError>()],
observer: FailingObserver(),
) I inspired and finished a working proposal in #4205 |
@Rodsevich thanks for the additional context. I'm not sure modifying |
@felangel thank for your response. The challenge relies on having your BlocObserver solution tested end to end. Take the example I gave you before, how should you test your analytics/logging/etc solution made for BLoCs is working without a test over a BLoC? You wanna ensure your BLoCs will behave as you expect, not your observers. |
Since these are unit tests, I'd argue that you should only be testing the Hope that helps! |
The thing is if we should have behavioral changing objects in the unitTesters or not, right? I think this is turning philosophical 🤔. I love it! |
@Rodsevich |
But they can, can't them? How do you recommend implementing something that tweaks the behavior of all your BLoCs? |
Can you provide some more context regarding the use-case your have? I'd love to learn more about how you're using |
Sure! I want a way of defining analytics on BlocEvents. For that I use this observer that checks if they are present on the events and send 'em. |
Wouldn't it be sufficient to have a unit test to make sure that you are overriding the |
If you want to unit test in general your BlocObserver, yes. But if you want to test your BLoCs, I don't think so... |
Description
ATM BlocObservers are being overriden in
blocTest
logic, and I'm not sure they work as expected when errors are thrown in them (for sure they aren't being outputted on the errors: matcher)Desired Solution
Add another parameter to blocTest with the oberserver they should use, fully functional ones.
Alternatives Considered
I found workarounds (like defining them on build() everytime and using runZoneGuarded in tests), but I think this is a nice to have, and not that hard to implement neither.
The text was updated successfully, but these errors were encountered: