ninghao / ninghao_flutter

宁皓网 《Flutter 移动应用》开发课程源代码
https://ninghao.net/package/flutter
412 stars 186 forks source link

集成测试的问题 #4

Open LLemonGreen opened 5 years ago

LLemonGreen commented 5 years ago

分支:test

貌似test.dart已经移除了,用test_api.dart替代了,但是使用test_api.dart包无法完成集成测试,报错如下:

Unhandled exception:
NoSuchMethodError: The method 'group' was called on null.
Receiver: null
Tried calling: group("App", Closure: () => Null, onPlatform: null, retry: null, skip: null, solo: false, tags: null, testOn: null, timeout: null)
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
#1      group (package:test_api/test_api.dart:179:13)
#2      main (file:///C:/Users/LLemo/Desktop/Flutter.toBeMaster/flutter.toBeMaster.step01/test_driver/app_test.dart:8:3)
#3      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:19)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
Stopping application instance.
Driver tests failed: 255

代码

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test_api/test_api.dart';

void main() {
  group('App', () {
    FlutterDriver driver;

    final actionChip = find.byValueKey('actionChip');
    final actionChipLabelText = find.byValueKey('actionChipLabelText');

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    tearDownAll(() async {
      if (driver != null) {
        driver.close();
      }
    });

    test('starts at 0', () async {
      expect(await driver.getText(actionChipLabelText), '0');
    });

    test('increaments the counter', () async {
      await driver.tap(actionChip);
      expect(await driver.getText(actionChipLabelText), '1');
    });
  });
}

然后脑子一抽打算用浅薄的理解曲线救国,导入包flutter_test.dart 然后代码报错如下提示:

The name 'find' is defined in the libraries 'package:flutter_driver/src/driver/driver.dart' and 'package:flutter_test/src/finders.dart'.
Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.dart(ambiguous_import)

继续如下暴力执法,解决代码报错

import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_test/flutter_test.dart' hide group, find;
import 'package:test_api/test_api.dart' show group;

以为大功告成,结果终端报错如下:

file:///C:/flutter/packages/flutter_test/lib/src/accessibility.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///C:/flutter/packages/flutter_test/lib/src/binding.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///C:/flutter/packages/flutter_test/lib/src/matchers.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///C:/flutter/packages/flutter_test/lib/src/matchers.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui';
       ^
file:///C:/flutter/packages/flutter_test/lib/src/test_pointer.dart:12:1: Error: Not found: 'dart:ui'
export 'dart:ui' show Offset;
^
file:///C:/flutter/packages/flutter_test/lib/src/window.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' hide window;
       ^
file:///C:/flutter/packages/flutter/lib/src/rendering/box.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show lerpDouble;
       ^
file:///C:/flutter/packages/flutter/lib/src/rendering/debug_overflow_indicator.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
       ^
file:///C:/flutter/packages/flutter/lib/src/rendering/editable.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show TextBox, lerpDouble;
       ^
file:///C:/flutter/packages/flutter/lib/src/rendering/error.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, TextStyle;
       ^
Stopping application instance.
Driver tests failed: 254

google查了一下,一脸懵逼,没能解决,先把坑记住,大丈夫能屈能伸,在pubspec.yaml改动如下:

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_driver:
    sdk: flutter
  test: ^1.6.1  #这里
  intl_translation: ^0.17.4

test 只能用小于1.6.1,大于1.6.1版本的提示如下:

Because every version of flutter_driver from sdk depends on test_api 0.2.4 and test >=1.6.2 depends on test_api 0.2.5, flutter_driver from sdk is incompatible with test >=1.6.2.

So, because flutter.toBeMaster.step01 depends on both test ^1.6.3 and flutter_driver any from sdk, version solving failed.
pub get failed (1)
Because every version of flutter_driver from sdk depends on test_api 0.2.4 and test >=1.6.2 depends on test_api 0.2.5, flutter_driver from sdk is incompatible with test >=1.6.2.

So, because flutter.toBeMaster.step01 depends on both test ^1.6.2 and flutter_driver any from sdk, version solving failed.
pub get failed (1)

我底子烂,暂时先妥协,然后终于可以导入包:

import 'package:flutter_driver/flutter_driver.dart';
// import 'package:flutter_test/flutter_test.dart' hide group, find;
// import 'package:test_api/test_api.dart' show group;
import 'package:test/test.dart';

终于顺利完成了教学视频中集成测试的结果。

阿西吧。。。。。

当前flutter版本:

guangyuanxia commented 5 years ago

你的日志是在哪里看到的?我用的是Android Studio 照你说的配置, 直接点击run按钮没有反应,也没看到哪里输出日志呢 @LLemonGreen

LLemonGreen commented 5 years ago

你的日志是在哪里看到的?我用的是Android Studio 照你说的配置, 直接点击run按钮没有反应,也没看到哪里输出日志呢 @LLemonGreen

@guangyuanxia 我用的VSCode,按理AS也应该一样输出日志,除非插件版本以及Flutter版本和我不同。。。