测试:
var assert = require(‘assert‘);
var now = Date.now();
console.log(now);
assert.ok(now % 2 == 0);
----------------------------------------
var request = require(‘superagent‘);
var assert = require(‘assert‘);
request.get(‘http://localhost:3000‘)
.send({q: ‘bieber‘})
.end(function (res) {
//断言判断响应状态码是否正确
assert.ok(200 == res.status);
//断言关键字是否存在
assert.ok(~res.text.toLowerCase().indexOf(‘bieber‘));
//断言列表项是否存在
assert.ok(~res.text.indexOf(‘<li>‘));
});
expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be,ok();
be/equal: ===
expect(1).to.be(1); expect(NaN).not.to.equal(NaN); expect(1).not.to.be(true);
eql:断言非严格相等,支持对象
expect({a: ‘b‘}).to.eql({a: ‘b‘});
expect(1).to.eql(‘1‘);
expect(5).to.be.a(‘number‘); expect([]).to.be.an(‘array‘); expect([]).to.be.an(‘object‘); //constructor expect(5).to.be.a(‘Number‘); expect([]).to.be.an(‘Array‘); expect(tobi).to.be.a(Ferrect); //instanceof
expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
expect([1,2]).to.contain(1); expect(‘hello world‘).to.contain(‘world‘);
expect([]).to.have.length(0); expect([1,2,3]).to.have.length(3);
expect([]).to.be.empty(); expect([1,2,3]).to.not.be.empty();
expect(window).to.have.property(‘expect‘);
expect(window).to.have.property(‘expect‘,expect);
expect({a: ‘b‘}).to.have.property(‘a‘);
expect({a: ‘b‘}).to.have.key(‘a‘);
expect({a: ‘b‘, c: ‘d‘}).to.only.have.keys(‘a‘, ‘c‘);
expect({a: ‘b‘, c: ‘d‘}).to.only.have.keys([‘a‘.‘c‘]);
expect({a: ‘b‘, c: ‘d‘}).to.not.only.have.keys(‘a‘);
expect(fn).to.throwException(); expect(fn2).to.not.throwException();
expect(1).to.be.within(0,Infinity);
expect(3).to,be.above(0); expect(5).to.be.greaterThan(3);
expect(0).to.be.below(3); expect(1).to.be.lessThan(3);
Moncha: 测试框架
describe(‘a topic‘, function () {
it(‘should test something‘, function () {
});
describe(‘anthor topic‘, function () {
it(‘should test something else‘, function () {
})
})
});
it(‘should not know‘, function (done) {
setTimeout(function () {
assert.ok(1 == 1);
done();
}, 100);
});
如果一个测试用例中有许多异步操作,可以添加一个计数器:
it(‘should complete three requests‘, function (done) {
var total = 3;
request.get(‘http://localhost:3000/1‘, function (res) {
if(200 != res.status) throw new Error(‘Request error‘); --total || done();
});
request.get(‘http://localhost:3000/2‘, function (res) {
if(200 != res.status) throw new Error(‘Request error‘); --total || done();
});
request.get(‘http://localhost:3000/3‘, function (res) {
if(200 != res.status) throw new Error(‘Request error‘); --total || done();
});
})
suite(‘net‘, function () {
suite(‘Stream‘, function () {
var client;
suiteSetup(function () {
client = net.connect(3000, ‘localhost‘);
});
test(‘connect event‘, function (done) {
client.on(‘connect‘, done);
});
test(‘receiving data‘, function (done) {
client.write(‘‘);
client.once(‘data‘, done);
});
suiteTeardown( function () {
client.end();
})
})
})
exports.Array = {
‘#indecOf()‘ : {
‘should return -1 when the value is not present‘ : function () {},
‘should return the correct index when the value is present‘ : function () {}
}
}
在浏览器端使用Mocha:
原文:http://www.cnblogs.com/jinkspeng/p/4133441.html