Closed aoatmon closed 2 years ago
sorry for the wrong label :/
Hi @aoatmon 👋 Thanks for opening an issue!
I'm not sure I understand what you're trying to achieve. If you want to test your widget code (including providing blocs/cubits) then I highly recommend you use a widget test. If you want to test your blocs/cubits alone then I highly recommend you use bloc_test
.
I recommend taking a look at the counter example (it includes unit, widget, and integration tests).
Hope that helps 👍
hi @felangel I'm not trying to test a widget, but to
context.read<Foo>()
)
my current not working solution uses testWidgets
void contextTester({
required String title,
required Widget Function(Key) build,
required ContextTester tester,
}) {
final key = UniqueKey();
test.testWidgets(title, (_tester) async {
await _tester.pumpWidget(build(key));
final context = _tester.element(test.find.byKey(key));
await tester(context, _tester);
});
}
but it fails to find widget in the context (I'm aware the logs point more at a provider issue) but my question is more open ended than "please fix my errors"
I'm trying to find a consistent way to test this aspect of the package
@aoatmon BlocListener
and BlocProvider
are widgets so I highly recommend using testWidgets
as illustrated in the example I shared above.
To verify the interaction between cubits and listeners you can provide a mock cubit to the widget tree and use whenListen
from package:bloc_test
to stub the state stream.
To verify that a listener interacts with the context you can again provided a mock instance of the bloc or cubit and verify the correct API is called via verify
from package:mocktail
.
You can refer to the weather example tests for a complete example of what I described/recommend.
Hope that helps 👍
I put together this
it works, but I'm not sure I'm using this package as intended I would appreciate if someone could tell me if I'm doing something wrong
@aoatmon if it works for you feel free to keep using that approach but as I mentioned, I recommend following the patterns found in the examples. I can't provide much feedback because I don't feel I have enough context and there are objects like GoRouterBlocListener
which are not part of the bloc library.
Closing for now but if you have any other questions that pertain to packages in the bloc library and the recommended usage, then I'm happy to continue the conversation 👍
use case
I would like to structure my tests in a way that allow me to
listeners
on cubit changeissue
afaik the typical implementation for this is a full blown widget test which may be undesirable when we don't actually need to test the widgets.
I'm trying to streamline the process, so far with mixed results
(if you want to run the code you can just clone the repo)
code
```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_test/flutter_test.dart' as test; import 'mocks.dart'; typedef ContextTester = FutureOrpubspec
```yaml name: context_bloc_test description: looking for a way to test cubit from context publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=2.14.4 <3.0.0' flutter: ^2.5.3 dependencies: flutter: sdk: flutter flutter_bloc: ^8.0.0 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^1.0.4 bloc_test: ^9.0.1 flutter: uses-material-design: true ```doctor
```console [✓] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-x64, locale en-EE) • Flutter version 2.5.3 at /Users/francesco/fvm/versions/stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (6 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/francesco/Library/Android/sdk • Platform android-31, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.1, Build version 13A1030d • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) [✓] VS Code (version 1.62.3) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.28.0 [✓] Connected device (2 available) • macOS (desktop) • macos • darwin-x64 • macOS 12.0.1 21A559 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.55 • No issues found! ```logs
```bash ╭─francesco@francescos-MBP ~/development/context_bloc_test ╰─$ flutter test 00:08 +0: CounterBloc emits [] when nothing is added ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following ProviderNotFoundException was thrown building _NestedHook: Error: Could not find the correct Providerbottomline
I'd appreciate some suggestion on a better approach or if this approach makes any sense I could use some help to fix the issue
thank you