単体テストしたい Dart クラスがあり、dart:html ライブラリへの呼び出しをモックして、クラスが期待どおりに動作していることを確認したいと考えています。Mocking with Dartの記事を見ましたが、HTML ライブラリをモックする方法については言及されていません。誰かアドバイスはありますか?
1 に答える
1
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;
}
于 2012-11-03T11:14:18.673 に答える