Dart模仿函数 [英] Dart Mocking a Function
本文介绍了Dart模仿函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试被调用的模拟函数?
How do I test that a mocked function was called?
我在使用Dart进行模拟 - 如何测试作为参数传递的函数是否被调用? / a>并尝试扩展它以检查函数是否被调用。
I found this example on Mocking with Dart - How to test that a function passed as a parameter was called? and tried to extend it to check if the function was called.
library test2;
import "package:unittest/unittest.dart";
import "package:mock/mock.dart";
class MockFunction extends Mock {
call(int a, int b) => a + b;
}
void main() {
test("aa", () {
var mockf = new MockFunction();
expect(mockf(1, 2), 3);
mockf.getLogs(callsTo(1, 2)).verify(happenedOnce);
});
}
看起来 mockf.getLogs结构为空...
It appears that the mockf.getLogs() structure is empty...
推荐答案
您必须在日志中模拟方法并指定其名称。这里是工作代码:
You must mock methods and specify their names in the log. Here's working code:
library test2;
import "package:unittest/unittest.dart";
import "package:mock/mock.dart";
class MockFunction extends Mock {
MockFunction(){
when(callsTo('call')).alwaysCall(this.foo);
}
foo(int a, int b) {
return a + b;
}
}
void main() {
test("aa", () {
var mockf = new MockFunction();
expect(mockf(1, 2), 3);
mockf.calls('call', 1, 2).verify(happenedOnce);
});
}
编辑:
类似问题: Dart如何模拟过程
这篇关于Dart模仿函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文