【发布时间】:2017-07-11 17:51:11
【问题描述】:
我有一个函数可以从数组中返回一个 html 字符串“”,
试图为它写一个摩卡测试。但是如果我尝试断言相同,空格和 \n 会让我很难过,而且它也不能像字符串一样灵活地测试 html!。
如何为返回 html 字符串的纯函数编写测试?
示例:
renderHtml(json){
// .. function that parse json and return <table> of its content.
}
如何测试这个功能?
var mod = require('./render');
var count = (s1,s2) => (s1.match(new RegExp(s2, 'gi'))||[]).length;
describe('should handle simple array', function () {
let json = [{ "id": "1", "name": "momen" }];
let result = mod.renderHtml(json);
it('should have 1 h1', function () {
assert.equal(count(result, '<h1>'), 1);
})
it('should have 4 ths', function () {
assert.equal(count(result, '<th>'), 4);
})
it('should have 2 tr', function () {
assert.equal(count(result, '<tr>'), 2);
})
it('should have 2 td', function () {
assert.equal(count(result, '<td>'), 2);
})
})
我想测试结果是否包含标签,以及包含 X 个 ths 和 X 个数的表或每行包含 X 个 tds 的行
【问题讨论】:
标签: javascript unit-testing tdd mocha.js chai