Node関連--テスト
6128 ワード
テスト: assertモジュール;//node持参
ecpect.js、assertのコードの書き方を最適化します;API: ok:断言が真実かどうか:
be/equal: ===
eql:断言は厳密に等しくなく、サポート対象
a/an:属する属性タイプを断言し、配列のinstanceofをサポートする
match:断言文字列が正規表現に一致するかどうか
contain:断言文字列に別の文字列が含まれているかどうか.
length:配列長を断言する;
empty:配列が空であるかどうかを断言する.
property:自分の属性/値が存在するかどうかを断言する.
key/keys:断言キーが存在するかどうか、only修飾子をサポートする.
throwException:Functionが呼び出し時に異常を投げ出すかどうかを断言する.
within:配列がある区間内にあるかどうかを断言する.
greaterThan/above: >
lessThan/below: <
Moncha:テストフレームワーク例: test.js
運転:mocha test.js ;レポートリスト形式:mocha-R list test.js
非同期コードのテスト:Mochaのデフォルトは、1つのテスト・インスタンスが実行された直後に別のコードを実行します.しかし、次のテスト例の実行を遅らせたい場合があります.
1つのテスト・インスタンスに多くの非同期操作がある場合は、カウンタを追加できます.
BDDスタイル:前のテスト例スタイルはBDD(動作駆動開発); TDDスタイル:テスト駆動開発、組織方式はテストセット(suit)とテスト(test)を使用する.各テストセットにはsetupとteardowm関数があり、これらの方法はテストセットのテストが実行される前に実行され、コードの重複がテスト間で互いに独立することを最大限に避けるために実行されます.
exportスタイル:nodeモジュールシステムを使用してテストを出力します.各exportのキーはテストセットを表し、ネストされたテストセットはサブオブジェクトで表すことができます.
ブラウザ側でのMochaの使用:例
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();
expect(1).to.be(1);
expect(NaN).not.to.equal(NaN);
expect(1).not.to.be(true);
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);
});
1つのテスト・インスタンスに多くの非同期操作がある場合は、カウンタを追加できます.
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の使用:例