我有一个Dart类,我想进行单元测试,我想模拟对dart:html库的调用,以确保我的类按预期运行.我查看了
Mocking with Dart的文章,但没有提到如何模拟HTML库.有人有建议吗?
解决方法
这不容易实现,因为dart:html库不是无头的(即它需要一个浏览器).我通常尝试遵循MVP设计模式,以确保与DOM交互的代码仅在我的视图类中,并且所有商业逻辑都在演示者中.这样我就可以在不需要访问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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。