颤振单元测试。测试时出错

时间:2019-06-18 16:08:49

标签: unit-testing flutter

运行颤振单元测试时出现以下错误。

  

00:05 +1:...   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart   Shell:[错误:flutter / shell / common / shell.cc(184)] Dart错误:错误:   dart进口:带镜子   --enable-mirrors = false Shell:[错误:flutter / shell / common / engine.cc(175)]无法准备运行   隔离。 Shell:[ERROR:flutter / shell / common / engine.cc(122)]引擎   不准备并启动隔离。贝壳:   [错误:flutter / shell / testing / tester_main.cc(199)]无法启动   配置引擎。 00:05 +1 -1:加载   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart   [E]加载失败   “ /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart”:   Shell子进程在连接到测试之前干净地报告了一个错误   马具。检查上面的日志以获取错误消息。测试:   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart   贝壳:   /home/kazekage/Development/flutter_linux_v1.2.1-stable/flutter/bin/cache/artifacts/engine/linux/x64/flutter_tester         Shell:[错误:flutter / shell / common / shell.cc(184)] Dart错误:错误:使用以下命令导入了dart:mirrors   --enable-mirrors = false Shell:[错误:flutter / shell / common / engine.cc(175)]无法准备运行   隔离。 Shell:[ERROR:flutter / shell / common / engine.cc(122)]引擎   不准备并启动隔离。贝壳:   [错误:flutter / shell / testing / tester_main.cc(199)]无法启动   配置引擎。 00:05 +1 -2:加载   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart   [E]加载失败   “ /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart”:   Shell子进程在连接到测试之前干净地报告了一个错误   马具。检查上面的日志以获取错误消息。测试:   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart   贝壳:   /home/kazekage/Development/flutter_linux_v1.2.1-stable/flutter/bin/cache/artifacts/engine/linux/x64/flutter_tester         Shell:[错误:flutter / shell / common / shell.cc(184)] Dart错误:错误:使用以下命令导入了dart:mirrors   --enable-mirrors = false Shell:[错误:flutter / shell / common / engine.cc(175)]无法准备运行   隔离。 Shell:[ERROR:flutter / shell / common / engine.cc(122)]引擎   不准备并启动隔离。贝壳:   [错误:flutter / shell / testing / tester_main.cc(199)]无法启动   配置引擎。 00:06 +1 -3:加载   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart   [E]加载失败   “ /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart”:   Shell子进程在连接到测试之前干净地报告了一个错误   马具。检查上面的日志以获取错误消息。测试:   /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart   贝壳:   /home/kazekage/Development/flutter_linux_v1.2.1-stable/flutter/bin/cache/artifacts/engine/linux/x64/flutter_tester

pubsec.yml

name: flutter_woocommerce
description: A new Flutter package project.
version: 0.0.1
author:
homepage:

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  dio: ^2.1.6
  built_value: ^6.6.0
  built_value_generator: ^6.6.0
  built_collection: ^4.2.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  mockito: ^4.0.0
  build_runner: ^1.5.0
  source_gen: ^0.9.4+2

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # To add assets to your package, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  #
  # For details regarding assets in packages, see
  # https://flutter.io/assets-and-images/#from-packages
  #
  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # To add custom fonts to your package, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts in packages, see
  # https://flutter.io/custom-fonts/#from-packages

颤抖的医生

[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Linux, locale en_US.UTF-8)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Android Studio (version 3.4)
[!] IntelliJ IDEA Ultimate Edition (version 2018.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.35.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

Tag.dart

import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:flutter_woocommerce/model/util/Link.dart';

part 'Tag.g.dart';

abstract class Tag implements Built<Tag, TagBuilder> {
  static Serializer<Tag> get serializer => _$tagSerializer;

  int get id;

  String get name;

  String get slug;

  @nullable
  String get description;

  @nullable
  int get count;

  @nullable
  @BuiltValueField(wireName: '_links')
  Link get links;

  Tag._();

  factory Tag([updates(TagBuilder b)]) = _$Tag;
}

Tag_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'dart:convert';
import 'package:built_collection/built_collection.dart';
import 'package:flutter_woocommerce/test_strings.dart';
import 'package:flutter_woocommerce/model/Serializers.dart';
import 'package:flutter_woocommerce/model/Tag.dart';
import 'package:flutter_woocommerce/model/util/Link.dart';

void main() {

  final parsedJson = json.decode(TestStrings.TAG_JSON);
  Tag tag = serializers.deserializeWith(Tag.serializer, parsedJson);

  test('id', () {
    expect(tag.id, 32);
  });

  test('name', () {
    expect(tag.name, 'Telephone');
  });

  test('slug', () {
    expect(tag.slug, 'telephone');
  });

  test('description', () {
    expect(tag.description, '');
  });

  test('count', (){
    expect(tag.count, 0);
  });

  test('_links is Link Object', () {
    expect(tag.links, isInstanceOf<Link>());
  });

  test('_links > self is href[]', () {
    expect(tag.links.self, isInstanceOf<BuiltList<Href>>());
  });

  test('_links > collection is href[]', () {
    expect(tag.links.collection, isInstanceOf<BuiltList<Href>>());
  });

  test('_links > collection > href value', () {
    expect(tag.links.collection.first.href, 'http://paathsala-wp.test/wp-json/wc/v2/products/tags');
  });
}

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试使用反射(dart:mirror),这在Flutter中是不可能的,因为:

  

“由于反射默认使所有代码都隐式使用,因此会干扰树的摇动。” Source

我相信它与built_value_generator dart软件包提供的JSON序列化有关。

答案 1 :(得分:0)

似乎我在一个模型类中导入了错误的库。一旦我删除了图书馆

import 'package:built_collection/built_collection.dart';

所有测试都通过了。