本文作者:IMWeb 何璇 原文出处:IMWeb社区 未经同意,禁止转载
TOC
在使用node开发iconfont平台时,由于没有产品与设计的主导,我遇到了协同开发的一大难题——合并代码。开发过程中每次合并代码时基本上都有冲突,在手动解决冲突的过程中,随着代码量的增大,解决过程我真是如履薄冰,生怕改错了逻辑,导致一些原本的功能出错等后果。而每次合并完提交前,都要将所有的功能手动测试一遍,费时费力。
基于以上的原因,编写测试来保证应用的健壮性,减低协同开发的成本是非常有必要的。而且,node社区已经有成千上万的开源模块,当开发者使用第三方模块时,没有提供测试的第三方模块值得信赖嘛?对于开发者而言,应该对自己产出的代码负责。
单元测试主要包含断言,测试框架,测试用例,测试覆盖率,mock,持续集成等几个方面,在用Mocha对node应用进行测试时,我以下面几个方面为例进行介绍:
Mocha is a feature-rich JavaScript test framework running on Node.js and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub. —— MochaJS
可以在其官网介绍中看出,Mocha是具有强大测试功能的测试框架:
断言(assertion)是一种放在程序设计中的一阶逻辑(如一个结果为真或为假的逻辑判断式)
Mocha支持你用任何一种断言库,无论是should.js、chai、expect.js、better-assert等等。也可以用node原始的assert。这里我们就用expect.js:
expect(window.r).to.be(undefined);
expect({ a: 'b' }).to.eql({ a: 'b' })
expect(5).to.be.a('number');
expect([]).to.be.an('array');
expect(window).not.to.be.an(Image);
Mocha支持BDD(行为驱动开发)和TDD(测试驱动开发)两种测试风格,BDD对于TDD来说在关注点更关注整体行为是否符合预期,在表达方式上更接近于自然语言的习惯。Mocha的默认模式是BDD,在此我们只关注BDD模式。
BDD风格的钩子函数有:before, after, beforeEach, afterEach
典型BDD风格测试:
var assert = require("assert");
describe('Array', function(){
before(function() {
// ...
})
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
})
})
after(funtion() {
// ...
})
});
在此我以测试iconfont平台首页的展示功能为例: 注意编写测试代码时最重要的两件事就是:
在下面的代码中,我是以测试路由接口的形式,通过测试返回的html字符串与构造的mock数据相比的方法来测试的。在实际应用中,有远比这展示功能复杂的功能,比如搜索功能,可以通过rewire来获取routes/search.js中私有方法search
,来测试,比较回调函数中参数对象。
代码如下:
var http = require('http'),
fs = require('fs');
var expect = require('expect.js'),
ejs = require('ejs'),
rewire = require('rewire'),
EventEmitter = require('eventemitter2').EventEmitter2,
emitter = new EventEmitter();
var Icon = require('../models/icon.js'),
Business = require('../models/Business.js'),
svgParser = require('../utils/svg_parser.js');
var options = {
host: 'localhost',
port: '4001',
path: '/',
method: 'GET'
};
var objs = [
{name: 'PC 平台', pm: 'moxhe'},
{name: 'H5公众账号', pm: 'moxhe'},
{name: '上课web化', pm: 'moxhe'}
];
var icons = [
[{name: 'right', path: '/right.svg'}],
[{name: 'search', path: '/search.svg'}],
[{name: 'list', path: '/list.svg'}]
];
var renderParams = {
all: {},
user: undefined,
bMaps: {}
};
describe('router/main.js', function(){
before(function (done) {
// tell mocha disable timeouts
// default is 2000
this.timeout(0);
Icon.remove(function () {
Business.remove(function () {
objs.forEach(function (obj) {
(new Business(obj)).save(function (err, doc) {
bids.push(doc.bid);
renderParams.bMaps[doc.bid] = doc.name;
emitter.emit('bid ready');
});
});
});
});
var bids = [];
var count = 0;
emitter.on('bid ready', function () {
count++;
if (count !== objs.length) return;
icons.forEach(function (arr, index) {
var bid = bids[index];
arr.forEach(function (icon) {
icon.business = bid;
var icon = new Icon(icon);
icon.save(function (err, doc) {
if (!renderParams.all[doc.business]) {
renderParams.all[doc.business] = [];
}
// special inject
doc.content = svgParser.generateHtmlIconContent(doc.iconId);
renderParams.all[doc.business].push(doc);
emitter.emit('insert success');
});
});
});
});
var count2 = 0;
emitter.on('insert success', function () {
count2++;
if (count2 >= 3) {
console.log('db ready!');
done();
}
});
});
describe('router ["/", "/index"]', function() {
it('should display all icons in business order', function (done) {
this.timeout(5000);
// var getAllIcons = rewire('../routes/main.js').__get__('getAllIcons');
// getAllIcons(function (err, icons, ret) {
// expect(icons).to.eql(renderParams.all);
// done();
// });
var content = fs.readFileSync('../views/index.html').toString();
renderParams.filename = __dirname + '/../views/index.ejs';
var html = ejs.render(content, renderParams);//.replace(/\s/g, '');
http.get(options, function (res) {
var str = '';
res.on('data', function (trunk) {
str += trunk;
});
res.on('end', function () {
expect(str).to.eql(html);
// expect(str.replace(/\s/g, '')).to.eql(html);
done();
});
});
});
});
after(function() {
// ...
});
});
让我们来看看运行的结果:
编写测试用例也是一门重要的学问,所谓测试驱动开发,本应该先写测试后开发,从而保证应用的健壮性,当然这个应用也必须足够分量。我觉得这还是蛮科学的,但是身边普遍部署测试的时候都是不得不部署的时候才开始的。相信当实践经验足够丰富时,对各种业务逻辑足够熟悉时就能科学地开发吧!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有