4
我有一个飞镖类,我想单元测试,我想嘲笑到镖电话:HTML库,以确保如期我的类行为。我看过的文章Mocking with Dart但它简化版,提到如何嘲笑HTML库。有人有建议吗?飞镖嘲讽HTML库
我有一个飞镖类,我想单元测试,我想嘲笑到镖电话:HTML库,以确保如期我的类行为。我看过的文章Mocking with Dart但它简化版,提到如何嘲笑HTML库。有人有建议吗?飞镖嘲讽HTML库
这并不容易,因为dart:html库不是无头的(即它需要浏览器)。我通常会尽量按照MVP的设计模式,以确保与DOM的交互的代码仅在我看来类和所有BIZ逻辑是在主持人。这样我就可以单元测试演示者而无需访问DOM API。下面是一个小例子。
// view interface has no reference to dart:html
abstract class View {
hello();
}
// view impl uses dart:html but hands of all logic to the presenter
class ViewImpl implements View {
View(this._presenter) {
var link = new Element.html("<a href="">a link</a>");
link.on.click.add(_presenter.onClick());
body.nodes.add(link);
}
hello() {
body.nodes.add(new Element.html("<p>Hello from presenter</p>");
}
Presenter _presenter;
}
// presenter acts on the View interface and can therefor be tested with a mock.
class Presenter {
Presenter(this._view);
onClick() => _view.hello();
View _view;
}
dart:html也包含非UI类,例如localStorage和sessionStorage,它们是客户端持久性的基础。我想确保我的数据存储和加载例程正常工作,并且不会退化。我想嘲笑存储类,但不能beause,嘲笑它,我必须导入镖:在我的单元测试文件的HTML。但是Dart的酒吧系统不会允许这样做。相反,它提供了错误:“不知道怎么装‘镖:HTML’”。有没有一种方法来模拟从DART类:HTML不导入镖:HTML(在单元测试)? – devdanke