Is there is any way to capture print() statement during integration test - flutter

Is there is any way during the integration testing to monitor or save or read whatever the print() statement is printing to the console. I m using integration_test for testing.

Maybe this will help you?
import 'dart:async';
import 'dart:developer';
void main(List<String> args) async {
final printed = <String>[];
final result = runZoned(() => foo(), zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
printed.add(line);
},
));
print('Result: $result');
print('Printed:\n${printed.join('\n')}');
debugger();
}
int foo() {
print('Hello');
print('Goodbye');
return 41;
}
P.S.
I added a debugger call so that the result of the work was visible. This statement can (and should) be removed.

Related

Use a specific instance of a class inside an isolate

I am using an isolate through the compute() method to fetch, parse and sort datas from an API (around 10k entries).
My method getAllCards() is defined inside a class YgoProRepositoryImpl which has an instance of my remote datasource class YgoProRemoteDataSource it is in this class that the method to call my API is defined (it is a simple GET request).
Code Sample
ygopro_repository_impl.dart
class YgoProRepositoryImpl implements YgoProRepository {
final YgoProRemoteDataSource remoteDataSource;
// ...
YgoProRepositoryImpl({
required this.remoteDataSource,
// ...
});
// ...
static Future<List<YgoCard>> _fetchCards(_) async {
// As I'm inside an isolate I need to re-setup my locator
setupLocator();
final cards = await sl<YgoProRemoteDataSource>()
.getCardInfo(GetCardInfoRequest(misc: true));
cards.sort((a, b) => a.name.compareTo(b.name));
return cards;
}
#override
Future<List<YgoCard>> getAllCards() async {
final cards = await compute(_fetchCards, null);
return cards;
}
// ...
}
service_locator.dart
import 'package:get_it/get_it.dart';
import 'data/api/api.dart';
import 'data/datasources/remote/ygopro_remote_data_source.dart';
import 'data/repository/ygopro_repository_impl.dart';
import 'domain/repository/ygopro_repository.dart';
final sl = GetIt.instance;
void setupLocator() {
// ...
_configDomain();
_configData();
// ...
_configExternal();
}
void _configDomain() {
//! Domain
// ...
// Repository
sl.registerLazySingleton<YgoProRepository>(
() => YgoProRepositoryImpl(
remoteDataSource: sl(),
// ...
),
);
}
void _configData() {
//! Data
// Data sources
sl.registerLazySingleton<YgoProRemoteDataSource>(
() => YgoProRemoteDataSourceImpl(sl<RemoteClient>()),
);
// ...
}
void _configExternal() {
//! External
sl.registerLazySingleton<RemoteClient>(() => DioClient());
// ...
}
The code is working properly but getAllCards() is not testable as I cannot inject a mocked class of YgoProRemoteDataSource inside my isolate because it will always get a reference from my service locator.
How can I do to not rely on my service locator to inject YgoProRemoteDataSource inside my isolate and make getAllCards() testable ?
Did a more serious attempt, please see the repo: https://github.com/maxim-saplin/compute_sl_test_sample
Essentially with the current state of affairs with Flutter/Dart you can't pass neither closures nor classes containing closures across isolates boundaries (yet that might change when newer features in Dart land Flutter https://github.com/dart-lang/sdk/issues/46623#issuecomment-916161528). That means there's no way you can pass service locator (which contains closures) or trick the isolate to instantiate a test version of locator via closure IF you don't want any test code to be part of the release build. Yet you can easily pass data source instance to isolate to be used at its entry point as a param.
Beside, I don't think asking isolate to rebuild the entire service locator makes sense. The whole idea behind compute() is to create a short leaving isolate, run the computation, return the result and terminate the isolate. Initialising the locator is an overhead which is better to be avoided. Besides it seems the whole concept of compute() is being as isolated from the rest of the app as possible.
You can clone the repo and run the tests. Few words about the sample:
Based on Flutter counter starter app
lib/classes.dart recreates the code snippet you provided
test/widget_test.dart verifies that YgoProRepositoryImpl is working fine with isolate running fake version of data source
YgoProRemoteDataSourceImpl mimics real implementation and is located at classes.dart and YgoProRemoteDataSourceFake mimics test version
Running isolates under flutter_test requires wrapping test body in tester.runAsync() in order to have real time async execution (rather than fake async used by default by tests and relying on pumping to progress test time). Running tests in this mode can be slow (there's actual 0.5 second wait), structuring the tests in a way when compute() is not used or tested not in many tests is reasonable
classes.dart
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
final sl = GetIt.instance;
class YgoCard {
YgoCard(this.name);
final String name;
}
abstract class YgoProRemoteDataSource {
Future<List<YgoCard>> getCardInfo();
}
class YgoProRemoteDataSourceImpl extends YgoProRemoteDataSource {
#override
Future<List<YgoCard>> getCardInfo() {
return Future.delayed(Duration.zero,
() => List.generate(5, (index) => YgoCard("Impl $index")));
}
}
abstract class YgoProRepository {
Future<List<YgoCard>> getAllCards();
}
class YgoProRepositoryImpl implements YgoProRepository {
final YgoProRemoteDataSource remoteDataSource;
YgoProRepositoryImpl({
required this.remoteDataSource,
});
static Future<List<YgoCard>> _fetchCards(
YgoProRemoteDataSource dataSource) async {
final cards = await dataSource.getCardInfo();
cards.sort((a, b) => a.name.compareTo(b.name));
return cards;
}
#override
Future<List<YgoCard>> getAllCards() async {
final cards = await compute(_fetchCards, remoteDataSource);
return cards;
}
}
void setupLocator() {
sl.registerLazySingleton<YgoProRepository>(
() => YgoProRepositoryImpl(
remoteDataSource: sl(),
),
);
sl.registerLazySingleton<YgoProRemoteDataSource>(
() => YgoProRemoteDataSourceImpl(),
);
}
widget_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:test_sample/classes.dart';
import 'package:test_sample/main.dart';
void main() {
setUpAll(() async {
setupFakeLocator();
});
testWidgets('Test mocked data source', (WidgetTester tester) async {
// Wrapping with runAync() is required to have real async in place
await tester.runAsync(() async {
await tester.pumpWidget(const MyApp());
// Let the isolate spawned by compute() complete, Debug run might require longer wait
await Future.delayed(const Duration(milliseconds: 500));
await tester.pumpAndSettle();
expect(find.text('Fake 9'), findsOneWidget);
});
});
}
class YgoProRemoteDataSourceFake extends YgoProRemoteDataSource {
#override
Future<List<YgoCard>> getCardInfo() {
return Future.delayed(Duration.zero,
() => List.generate(10, (index) => YgoCard("Fake $index")));
}
}
void setupFakeLocator() {
sl.registerLazySingleton<YgoProRepository>(
() => YgoProRepositoryImpl(
remoteDataSource: sl(),
),
);
sl.registerLazySingleton<YgoProRemoteDataSource>(
() => YgoProRemoteDataSourceFake(),
);
}
Do you really need to test the getCards() function?
What are you really testing there? That compute works, sure hope the Dart SDK team has a test for this.
That leaves _fetchCards(), and setupLocator() doesn't need to be tested either, it is precondition for your test-logic. You want to change the setup for the test anyways.
So what you actually want to test is the fetching & sorting. Restructure this into a testable static function and setup your locator beforehand. Put a #visibleForTesting annotation on it.
And on a side-note, depending on how much you bind in your service locator, this could be huge overhead for just using the one repository afterwards.
Example:
static Future<List<YgoCard>> _fetchCards(_) async {
// As I'm inside an isolate I need to re-setup my locator
setupLocator();
return reallyFetchCards();
}
#visibleForTesting
static Future<List<YgoCard>> reallyFetchCards() async {
final cards = await sl<YgoProRemoteDataSource>()
.getCardInfo(GetCardInfoRequest(misc: true));
cards.sort((a, b) => a.name.compareTo(b.name));
return cards;
}
#override
Future<List<YgoCard>> getAllCards() async {
final cards = await compute(_fetchCards, null);
return cards;
}
Test:
// Setup SL and datasource
...
final cards = await YgoProRepositoryImpl.reallyFetchCrads();
// Expect stuff
As I understand you have two options, either inject the dependencies needed for static Future<List<YgoCard>> _fetchCards(_) async via parameters, or mock the object in the locator itself. I would go for the fist option, and have something like :
static Future<List<YgoCard>> _fetchCards(_,YgoProRemoteDataSource remote) async {
// No need to set up locator as you passed the needed dependencies
// setupLocator();
final cards = await remote
.getCardInfo(GetCardInfoRequest(misc: true));
cards.sort((a, b) => a.name.compareTo(b.name));
return cards;
}
#override
Future<List<YgoCard>> getAllCards() async {
final cards = await compute(_fetchCards, null);
return cards;
}
Edit
just updated the answer as its easier to edit this here than in the comments...
Hmm, the only workaround that I can think of is to pass the setupLocator() function as an argument to the class YgoProRepositoryImpl :
final Function setupLocator;
YgoProRepositoryImpl({
required this.remoteDataSource,
required this.setupLocator;
// ...
});
This way you could pass a mock that sets up your mock classes or the real setupLocator of your service_locator.dart. This might not be to elegant. But it should make it testable as now you can mock the setup and its not hardcoded in the function

how to access singleton objects inside Workmanager.executeTask's callback using flutter-workmanager

Creating List<String> mainList globally and adding one item in main() function. But inside the Workmanager.executeTask's callback mainList's lenth is still 0. Even the hashCode of the mainList is different.
Why this is happening?
How make the mainList as Singleton?
Tried printing the Isolate.current.debugName, it's always 'main'.
List<String> mainList=[];
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) {
print("mainList.length=${mainList.length}"); // always length is 0
print("Isolate.current.debugName = ${Isolate.current.debugName}");
return Future.value(true);
});
}
void main() {
Workmanager.initialize(
callbackDispatcher,
isInDebugMode: true,
);
mainList.add("String1");
print("mainList.length=${mainList.length}"); // length is 1
print("Isolate.current.debugName = ${Isolate.current.debugName}");
Workmanager.registerPeriodicTask("1", "simpleTask");
runApp(MyApp());
}
In ListenableWorker.startWork() a new FlutterEngin instance is created every time and executing Workmanager.executeTask's callback.
So no way for using singleton objects.
BackgroundWorker.kt

Is this Flutter (Dart) code robust re "async" usage or are futher null checks required?

Is the following Flutter (using Flame package too) code robust in the sense that, is there a risk "prefs" may not be available during "update" and further checks and balances should be put in? Suggested code change to make it robust?
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LightComponent extends SpriteComponent {
SharedPreferences prefs;
LightComponent() : super.fromImage(Vector2(404/4,406/4), Flame.images.fromCache('jupiter_404×406.png'),)
Future<void> onLoad() async {
prefs = await SharedPreferences.getInstance();
}
void render(Canvas c) {
super.render(c);
}
void update(double t) {
super.update(t);
bool showWindow = prefs.getBool('showWindow'); // <== IS THIS OK, OR IS THERE Pref == null potential issue to cover???
// etc
}
}
The engine waits for onLoad() to be done before it adds the component to the update loop, so your code is the preferred way to do initialization of things that need to be loaded within components. So since update won't be called on the component before onLoad() has finished, your code should be safe without any extra checks inside of update.
you can make it a global variable and initialize in your main and use it everywhere.
SharedPreferences storage;
Future<Null> main()async {
WidgetsFlutterBinding.ensureInitialized();
storage = await SharedPreferences.getInstance();
runApp(App());
}
I too do this for my flame projects.

Stubbed function always returns null

Currently I'm following a tutorial of Resocoder (DDD Course). In this course there is an abstract class called i_auth_facade.dart, which contains a function getSignedInUser.
I have mocked the class and trying to get a valid option of user back from the getSignedInUser method, but the result is always null.
Also when I try to execute the authCheckRequested method of the AuthBloc, which then in the background invokes the getSignedInUser method, I get back null.
I must have some error with Mockito, as it seems the stub doesn't work.
Can anybody point me into the right direction why I don't get back the optionOf(User), but null instead?
import 'package:bloc_test/bloc_test.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:teach_mob_student/application/auth/auth_bloc.dart';
import 'package:teach_mob_student/domain/auth/i_auth_facade.dart';
import 'package:teach_mob_student/domain/auth/user.dart';
import 'package:teach_mob_student/domain/auth/value_objects.dart';
import 'package:teach_mob_student/domain/core/value_objects.dart';
class MockIAuthFacade extends Mock implements IAuthFacade {}
void main() {
final tValidUser = User(
id: UniqueId(),
name: StringSingleLine('Test User'),
emailAddress: EmailAddress('test#user.com'));
test('Should return valid option of user', () async {
// This is to check, if optionOf actually returns a user --> Yes it does
Option<User> tDirectResult = optionOf<User>(tValidUser);
// Here I stub the getSignedInUser method. If it is executed, I want to receive a valid user.
when(MockIAuthFacade()
.getSignedInUser()
).thenAnswer((_) async => Future.value(optionOf<User>(tValidUser)));
// Here I execute the method, and I would expect to get Future<Option<User>> back, but I only get back null.
final facade = MockIAuthFacade();
Option<User> tStubbedResult = await facade.getSignedInUser();
print(tDirectResult);
print(tStubbedResult);
});
// Here I do the same, but I execute the authCheckRequested method of the AuthBloc, which executes the
// getSignedInUser method. Also here I get back null as result.
blocTest('Should return valid option of user.',
build: () async {
when(MockIAuthFacade()
.getSignedInUser()
).thenAnswer((_) async => Future.value(optionOf<User>(tValidUser)));
return AuthBloc(MockIAuthFacade());
},
act: (bloc) async {
bloc.add(AuthEvent.authCheckRequested());
},
expect: [
]
);
}

Flutter - Load assets for tests

Not sure if it's a limitation or something, but below code does not load anything. I have some data driven behaviour that I'd like to test isolated.
class Loader
import 'dart:async';
import 'package:flutter/services.dart' show rootBundle;
class Loader {
Future<String> load() async{
return await rootBundle.loadString('assets/json/sketch.json');
}
}
The test
testWidgets('Should parse load sketch.json', (WidgetTester tester) async {
var loaderFuture = new Loader();
Future<String> resultFuture = loaderFuture.load();
resultFuture.then((value) => print(value))
.catchError((error) => print(error));
while(true){};
});
Future does not return neither success nor error and hangs forever. I know
the while(true) locking up the test, but for now I just wanted to see sketch.json printed
Asset location
To use rootBundle in your tests you need this at the beginning of your test programs:
import 'package:flutter_test/flutter_test.dart';
...
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
See the documentation of DefaultAssetBundle it describes using it and a AssetBundle to provide your own assets.
Create a class that wraps the rootBundle:
#injectable
class AssetsManager {
Future<String> loadString(String path) {
return rootBundle.loadString(path);
}
}
Then inject it to your class and in your test override its dependency:
getIt.unregister<AssetsManager>();
getIt.registerSingleton<AssetsManager>(AssetsManagerMock());
Based on your test scenario, Configure what will be returned when calling loadString by using Mocktail's when method.
I'm using get_it for DI. Hope it's clear enough.