testWithGame<T extends FlameGame<World>> function

  1. @isTest
Future<void> testWithGame<T extends FlameGame<World>>(
  1. String testName,
  2. CreateFunction<T> create,
  3. AsyncGameFunction<T> testBody, {
  4. Timeout? timeout,
  5. dynamic tags,
  6. dynamic skip,
  7. Map<String, dynamic>? onPlatform,
  8. int? retry,
})

Utility function for writing tests that require a custom game instance.

This function creates the game instance, initializes it, then passes it to the user-provided testBody, and in the end disposes of the game object.

Example of usage:

testWithGame<MyGame>(
  'MyComponent can be added to MyGame',
  () => MyGame(mySecret: 3781),
  (MyGame game) async {
    final component = MyComponent()..addToParent(game);
    await game.ready();
    expect(component.isMounted, true);
  },
);

Implementation

@isTest
Future<void> testWithGame<T extends FlameGame>(
  String testName,
  CreateFunction<T> create,
  AsyncGameFunction<T> testBody, {
  Timeout? timeout,
  dynamic tags,
  dynamic skip,
  Map<String, dynamic>? onPlatform,
  int? retry,
}) async {
  test(
    testName,
    () async {
      final game = await initializeGame<T>(create);
      await testBody(game);

      game.onRemove();
    },
    timeout: timeout,
    tags: tags,
    skip: skip,
    onPlatform: onPlatform,
    retry: retry,
  );
}