Skip to content

Async operations don't complete inside testWidgets() #72

Closed
@markstory

Description

@markstory

I'm working on my first flutter application, so I may have missed an obvious solution to this. I'm running into problems interacting with json_cache + localstorage within the context of tests. I've attempted to create a minimal reprodction.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:json_cache/json_cache.dart';
import 'package:localstorage/localstorage.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('database insert', (tester) async {
    final LocalStorage storage = LocalStorage('test-idea');
    var database = JsonCacheMem(JsonCacheLocalStorage(storage));
    print('before');
    await database.refresh('test', {'k': 'v'});
    print('set value');
  });
}

When I run this test with flutter test it prints before but never prints set value. However, if I use test() instead of testWidgets() the test completes successfully. The broader context of what I'm trying to do is, write a more complex test case where I display a form. That form contains dropdown menus that contain data that would be read from json_cache, but I'm not currently able to get data into json_cache.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions