Dart Mocking HTML Library

I have a Dart class that I would like to use unit test, and I would like to mock calls in the dart: html library to make sure my class behaves as expected. I looked at an article for Mocking with Dart , but doesn't mention how to mock HTML libraries. Does anyone have any advice?

+4
source share
1 answer

This is not easy to do because the dart: html library is not headless (i.e. it needs a browser). I usually try to follow the MVP design pattern to make sure that the code that interacts with the DOM is only in my view class and that all the biz logic is in the lead. Thus, I am a unit test lead, not needing access to the DOM API. Below is a small example.

// 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; } 
+1
source

Source: https://habr.com/ru/post/1440869/


All Articles