mocha.js: 3.4.2
chai.js: 4.1.0
node.js: 6.10.3
npm: 5.3.0
後から動作しません。テストハーネスはmocha.js/chai.js
に依存しています。 httpsでエンドポイントを呼び出す各テストは機能します。同様に、before()
とafter()
関数が呼び出されます。ただし、after()
関数内のRESTfulエンドポイントのいずれも起動することは不可能です。どうして?モカ/ chai.jsテストハーネス:httpsの呼び出しは、私がnode.js.の下で展開されたRESTfulサービスのための簡単なテストハーネスを設定した()関数
after()
がテストの最後に来ているので、私はRESTfulエンドポイントの非同期呼び出しが、テストプロセスが終了する前に戻る時間がないと考えました。だから、応答時間を与えるためにbusy-waitループをコード化しました。いいえ:エンドポイント呼び出しは単にafter()
では機能しません。ここで
package.json
です:
{
"name": "so-mcv-example",
"version": "1.0.0",
"description": "Minimum, Complete, and Verifiable Example",
"main": "index.js",
"dependencies": {
"restify": "^4.3.0"
},
"engines": {
"node": "6.10.3",
"npm": "5.3.0"
},
"devDependencies": {
"chai": "^4.1.0",
"chai-http": "^3.0.0",
"mocha": "^3.4.2"
},
"scripts": {
"test": "mocha"
}
}
私index.js
は、次のようになります。
const restify = require('restify'),
fs = require('fs');
const server = restify.createServer({
certificate: fs.readFileSync('security/server.crt'),
key: fs.readFileSync('security/server.key'),
name: 'so-mcv-example'
});
server.listen(443, function() {
console.log('%s listening at %s', server.name, server.url);
});
server.pre(restify.pre.userAgentConnection());
server.get('/status', function(req, res, next) {
res.send({ status: 'so-mcv-example is running ok.' });
return next();
});
test
ディレクトリの下に、私が持っているmocha.opts
:
--require ./test/test.bootstrap
と
test.bootstrap.js
:
process.env.NODE_ENV = 'test';
// CAUTION: never do this in production! For testing purposes, we ignore
// faults in the standard SSL handshaking.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
後者の設定がindex.js
で使用自己署名SSL証明書を交渉します。 test/status-test.js
では、私が持っている:
const chai = require('chai');
const expect = chai.expect;
const should = chai.should();
const server = 'https://localhost';
chai.use(require('chai-http'));
before(function() {
if (process.env.NODE_ENV != "test") {
this.skip();
}
// XXX Wipe the database.
console.log("Wipe the database BEFORE tests.");
});
describe('GET /status', function() {
it('passes, as expected', function(done) {
chai.request(server)
.get('/status')
.end(function(err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
});
after(function() {
if (process.env.NODE_ENV != "test") {
this.skip();
}
chai.request(server)
.get('/status')
.end(function(err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
console.log("2nd call to /status returned.");
done();
});
});
確かに、after()
機能でエンドポイントを呼び出すためにchai
を使用することが不自然です。それはポイントではありません。要点は、console.log()
ディレクティブは決して実行されないということです。
> [email protected] test /Users/charlie/workspace/so-mcv-mocha
> mocha
Wipe the database BEFORE tests.
GET /status
✓ passes, as expected (44ms)
1 passing (56ms)
がconsole.log()
が実行されていない理由についての説明のために以下に私の答えを参照してください:それは私がnpm test
を実行すると、結果は、です。
は、問題を解決するには、私はmocha.opts
にオプション--no-exit
を追加しました:
> [email protected] test /Users/charlie/workspace/so-mcv-mocha
> mocha
Wipe the database BEFORE tests.
GET /status
✓ passes, as expected (43ms)
1 passing (55ms)
2nd call to /status returned.
あなたのテストコードを確認すると便利だと思います。 –