你如果跟我一樣导坟,看到類似的文檔秘蛇,教你怎么寫單元測(cè)試耳璧,https://github.com/dart-lang/test/blob/master/pkgs/test/README.md
就興沖沖的把如下代碼盗忱,往 Flutter 項(xiàng)目里的一貼
import "package:test/test.dart";
void main() {
test("String.split() splits the string on the delimiter", () {
var string = "foo,bar,baz";
expect(string.split(","), equals(["foo", "bar", "baz"]));
});
test("String.trim() removes surrounding whitespace", () {
var string = " foo ";
expect(string.trim(), equals("foo"));
});
}
肯定會(huì)報(bào)錯(cuò)治筒,說:Error: Could not resolve the package 'test' in 'package:test/test.dart'.
怎么辦屉栓?
有兩種辦法解決:
- 添加依賴
打開 Flutter 項(xiàng)目根目錄
里的pubspec.yaml
,找到 dev_dependencies (新項(xiàng)目大概在27行處)耸袜,在下面添加test: ^1.5.1
友多。
在命令行執(zhí)行:flutter packages get
,就能添加這個(gè)依賴堤框。
或者
- 使用 Flutter 自帶的依賴
將import "package:test/test.dart";
換為import 'package:flutter_test/flutter_test.dart';
最后域滥,重新運(yùn)行測(cè)試命令:flutter test