Mojito markup test 能使用模拟数据来呈现 mojit 视图,得到的结果可以用来验证使用 YUI 节点模块。若是使用 mojito test
指令,则由它得到的实力能够在 mojito 单元测试中运行。
使用步骤:
需要 mojito-markup-test
.
增加一个 mojit spec
.
呈现视图和验证标记.
YUI.add('CompositeMojit_view-tests', function(Y) { var suite = new YUITest.TestSuite('CompositeMojit'), A = YUITest.Assert, MarkupTest = Y.mojito.MarkupTest, //Timeout for async testing TIMEOUT = 1000; suite.add(new YUITest.TestCase({ name: 'view', 'test index': function () { var test = this, //describe the mojit to render spec = { //mojit type (dir name) type: 'CompositeMojit', //action to test action: 'index', //mojit params params: { url: { p: 'Really!' } }, //mojit config (to use with ac.config.get() ) config: { foo: 'bar' }, //app level config (to use with ac.app) appConfig: { some: 'config param' }, // We need to define the child mojits, // modules should be in the requires: [] as well to be loaded (see below) children: { 'child_mojit': { type: 'ChildMojit' } } }; MarkupTest.render(spec, function (err, markup, meta, node) { //rendering done, resume paused test and verify test.resume(function () { var div, em, i, a, text; A.isNull(err); //we have markup A.areNotEqual('', markup, 'Markup'); //we have meta A.isNotNull(meta, 'Meta'); //we have a node A.isNotUndefined(node, 'Parent node'); A.isNotNull(node, 'Parent node'); div = node.one('.msg'); A.isNotUndefined(div, 'msg <div>'); A.isNotNull(div, 'msg <div>'); em = div.one('em'); A.isNotUndefined(em, '<em>'); A.isNotNull(em, '<em>'); text = em.getContent(); A.areEqual('Mojito is working.', text, 'Rendered message <em>'); i = div.one('i'); A.isNotUndefined(i, '<i>'); A.isNotNull(i, '<i>'); text = i.getContent(); A.areEqual(spec.params.url.p, text, 'Rendered message <i>'); a = div.one('a'); A.isNotUndefined(a, '<a>'); A.isNotNull(a, '<a>'); text = a.getContent(); A.areEqual(spec.config.foo, text, 'Rendered message <a>'); }); }); //we need this for async tests, wait till rendering done or timeout test.wait(TIMEOUT); } })); YUITest.TestRunner.add(suite); }, '0.0.1', {requires: [ 'mojito-test', //test rendering utility 'mojito-markup-test' //parent mojit 'CompositeMojit', //child mojit 'ChildMojit' ]});
背景 单元测试的重要性,怎么说呢,按照流程规范来讲,每次上线前应该把单元测试都跑一遍,确认通过率是100%才可以。否则我们对现有代码做过修改后,仅依靠测试同学回归验证的话,很容易有场景遗漏,或者隐含未被发现的问题,而且修改公共部分的时候,可能测试的回归量也是很大的。但是,现实情况一般都是,业务代码都写不过来,哪有时间写单测,更别提有质量的单测。 我们目前项目单测的要求是,行覆盖率60%以上,通过率