简而言之,throwsA(anything)用dart进行单元测试不足以满足我的需求。如何测试特定的错误消息或类型?
这是我要捕获的错误:
class MyCustErr implements Exception {
String term;
String errMsg() => 'You have already added a container with the id
$term. Duplicates are not allowed';
MyCustErr({this.term});
}
Run Code Online (Sandbox Code Playgroud)
这是当前通过的断言,但是要检查上面的错误类型:
expect(() => operations.lookupOrderDetails(), throwsA(anything));
这就是我想做的:
expect(() => operations.lookupOrderDetails(), throwsA(MyCustErr));
我如何确保 ui(小部件)在 Flutter 中的小部件测试期间抛出异常。这是我的代码不起作用:
expect(
() => tester.tap(find.byIcon(Icons.send)),
throwsA(const TypeMatcher<UnrecognizedTermException>()),
);
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下错误
...
Expected: throws <Instance of 'TypeMatcher<UnrecognizedTermException>'>
Actual: <Closure: () => Future<void>>
Which: returned a Future that emitted <null>
Run Code Online (Sandbox Code Playgroud)
或者......我是否应该通过查找错误消息等来测试 UI 如何处理异常?
有人可以告诉我如何在小部件测试期间实现最重要的抖动错误,以便我可以检查自己的自定义错误。
我在网上看到一些片段提到了这一点,但是我所有的实现都失败了
void main() {
testWidgets('throws an error when scanning unknown term types', (WidgetTester tester) async {
await tester.pumpWidget(injectTestWidget(new ItemScanScreen()));
await tester.enterText(find.byKey(new Key('term')), '');
await tester.tap(find.byIcon(Icons.send));
await tester.pump();
expect(
tester.takeException(),
isInstanceOf<UnrecognizedTermException>(),
reason: 'should have thrown an UnrecognizedTermException error but didn\'t',
);
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码失败,并显示以下消息,即使它看起来实际上“抓住”了我的错误:
The following UnrecognizedTermException was thrown running a test:
Instance of 'UnrecognizedTermException'
...
Run Code Online (Sandbox Code Playgroud)
我读到您可以执行以下代码段,但未看到如何/在哪里实现:
final errorHandled = expectAsync0((){});
FlutterError.onError = (errorDetails) {
// handle error
errorHandled();
});
Run Code Online (Sandbox Code Playgroud) 鉴于这tester.enterText将使我能够TextField在抖动测试中输入文字,我该如何模拟按android键盘上的DONE键或在文本字段内按键盘上的ENTER键?
这也等同于检查是否按下了IOS / android键盘上的DONE按钮
Netsuite 说在创建自定义模块时包含模块 ID,如下所示:
define(module_id, [dependencies], function)
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,找不到模块:
TypeError: Cannot read property "XPELPCenterModule" from undefined (/path/to/mysuitelet.js#35)
Run Code Online (Sandbox Code Playgroud)
当我删除ID时,它起作用了???
Hello Human Coder
Run Code Online (Sandbox Code Playgroud)
我错过了什么。我用同样的方式称呼他们两个
function (ui, email, runtime, search, record, log, render, cache, crypto, file, pcenter) {
var p = new pcenter.XPELPCenterModule();
Run Code Online (Sandbox Code Playgroud)
.....
p.helloWorld('Human Coder');
Run Code Online (Sandbox Code Playgroud)
以下是模块代码示例:
define(["require", "exports"],
function (require, exports) {
var XPELPCenterModule = /** @class */ (function () {
function XPELPCenterModule(name) {
this.name = name;
}
XPELPCenterModule.prototype.helloWorld = function (name) {
return 'Hello ' + name;
};
return XPELPCenterModule;
}());
exports.XPELPCenterModule …Run Code Online (Sandbox Code Playgroud)