gpt4 book ai didi

unit-testing - flutter 单元测试。运行测试时出错

转载 作者:IT王子 更新时间:2023-10-29 06:56:32 25 4
gpt4 key购买 nike

我在运行 flutter 单元测试时遇到以下错误。

00:05 +1: ... /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart Shell: [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: error: import of dart:mirrors with --enable-mirrors=false Shell: [ERROR:flutter/shell/common/engine.cc(175)] Could not prepare to run the isolate. Shell: [ERROR:flutter/shell/common/engine.cc(122)] Engine not prepare and launch isolate. Shell: [ERROR:flutter/shell/testing/tester_main.cc(199)] Could not launch the engine with configuration. 00:05 +1 -1: loading /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart [E] Failed to load "/home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart": Shell subprocess cleanly reported an error before connecting to test harness. Check the logs above for an error message. Test: /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/Refund_test.dart Shell: /home/kazekage/Developement/flutter_linux_v1.2.1-stable/flutter/bin/cache/artifacts/engine/linux-x64/flutter_tester Shell: [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: error: import of dart:mirrors with --enable-mirrors=false Shell: [ERROR:flutter/shell/common/engine.cc(175)] Could not prepare to run the isolate. Shell: [ERROR:flutter/shell/common/engine.cc(122)] Engine not prepare and launch isolate. Shell: [ERROR:flutter/shell/testing/tester_main.cc(199)] Could not launch the engine with configuration. 00:05 +1 -2: loading /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart [E] Failed to load "/home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart": Shell subprocess cleanly reported an error before connecting to test harness. Check the logs above for an error message. Test: /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/LineItem_test.dart Shell: /home/kazekage/Developement/flutter_linux_v1.2.1-stable/flutter/bin/cache/artifacts/engine/linux-x64/flutter_tester Shell: [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: error: import of dart:mirrors with --enable-mirrors=false Shell: [ERROR:flutter/shell/common/engine.cc(175)] Could not prepare to run the isolate. Shell: [ERROR:flutter/shell/common/engine.cc(122)] Engine not prepare and launch isolate. Shell: [ERROR:flutter/shell/testing/tester_main.cc(199)] Could not launch the engine with configuration. 00:06 +1 -3: loading /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart [E] Failed to load "/home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart": Shell subprocess cleanly reported an error before connecting to test harness. Check the logs above for an error message. Test: /home/kazekage/Projects/flutter/flutter_woocommerce/test/model/util/ShippingLine_test.dart Shell: /home/kazekage/Developement/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 医生

[✓] 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.

标签.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');
});
}

最佳答案

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

"Since reflection makes all code implicitly used by default, it interferes with tree shaking." Source

我认为这与 built_value_generator dart 包提供的 JSON 序列化有关。

关于unit-testing - flutter 单元测试。运行测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com