简体   繁体   English

为什么`#import(“dart:unittest”)`无法运行?

[英]Why `#import(“dart:unittest”)` can't run?

I write some dart test code: 我写了一些飞镖测试代码:

#import("dart:unittest");
main() {
  test('this is a test', () {
    int x = 2+3;
    expect(x).equals(5);
  });
}

It doesn't display any error in dart editor, but when I press the "run" button, it reports: 它在飞镖编辑器中不显示任何错误,但是当我按下“运行”按钮时,它会报告:

Do not know how to load 'dart:unittest''file:///home/freewind/dev/dart/editor
/samples/shuzu.org/test/model_test.dart': 
Error: line 1 pos 1: library handler failed
#import("dart:unittest");
^

I see there is a "dart:unittest" library in my dart-sdk. 我看到我的dart-sdk中有一个“dart:unittest”库。 Why it can't be run? 为什么不能运行?

Unfortunately, the unittest library is not yet wired into the dart: namespace. 不幸的是,unittest库还没有连接到dart:namespace。 Until that happens, if it ever happens, you'll need to use a relative path to get to the unittest library. 在此之前,如果它发生,您将需要使用相对路径来访问unittest库。

Something like: 就像是:

#import('path-to-dart/lib/unittest/unitest.dart');

More examples are here: http://api.dartlang.org/unittest.html 更多示例如下: http//api.dartlang.org/unittest.html

This page keeps showing up in Google results for dart and unittest , so I thought I would add an update. 此页面一直显示在Google针对dartunittest结果中,因此我想我会添加更新。 The unittest library is now installed quite easily through pub , Dart's package manager. unittest库现在通过安装很容易pub ,飞镖的包管理器。 To do this, make sure that you: 为此,请确保您:

check Add pub support when you create a new Dart application. 检查创建新Dart应用程序时Add pub support Then add (or uncomment) the dependency for the unittest package in your pubspec.yaml file. 然后在pubspec.yaml文件中添加(或取消注释) unittest包的依赖pubspec.yaml That file should look like this: 该文件应如下所示:

name:  range
description:  A sample application

dependencies:
    unittest: { sdk: unittest }

Run pub install (although if you are using Dart Editor, this command should automatically get run for you). 运行pub install (尽管如果您使用Dart编辑器,此命令应自动为您运行)。 Then, in the file where you will be writing your tests, add this import declaration: 然后,在您要编写测试的文件中,添加以下导入声明:

import "package:unittest/unittest.dart";

And you should be good to go. 你应该好好去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM