0

package:gcloud/service_scope.dart私のアプリは、サービス スコープ ( )からオブジェクトにアクセスしていstorageServiceますss.register()

ここで、このスコープにアクセスし、サービス スコープに入れたいモック オブジェクトを使用する関数を単体テストしたいと考えています。

これを行う唯一の方法は、次のようにすべてのテストに登録することです。

var withServiceScope = (callback()) => ss.fork(() {
  // Register all services here
  return callback();
});
test('the description', () => withServiceScope(() async {
  // Call my function that can now access the service scope
}));

または、setUp()関数でそれを実行できるようにする方法があるので、テストごとにこの行を追加する必要はありませんか?

4

1 に答える 1

1

これにより、テストの作成が簡単になる場合があります (コードはテストされていません)。

import 'package:test/test.dart' as t;
import 'package:test/test.dart' show group;

var withServiceScope = (callback()) => ss.fork(() {
  // Register all services here
  return callback();
});

test(String description, Function testFunction) {
  t.test(description, () => withServiceScope(() async {
    testFunction();
  }));
}

main() {    
  test('the description', () async {
    // Call my function that can now access the service scope
  }));
}
于 2015-09-13T13:38:04.333 に答える