目标是记录api测试。这个测试是一种集成测试,它们将整个应用程序加载到其所有中间件,并拦截外部http调用并记录它们。
在Python世界中存在" WebTest"和" VCRPY"为了那个原因。
该应用:
'use strict';
const express = require('express');
const request = require('superagent');
var app = express();
app.get('/hammer/version', function(req, res) {
request
.get('http://httpbin.org/get')
.end(function(err, response) {
console.log(response.body);
res.status(200).json({
version: '0.1.0',
url: response.body.url
});
});
});
module.exports = app;
测试:
/* global describe, it */
'use strict';
const request = require('supertest');
const app = require('./app.js');
var path = require('path');
var tape = require('tape');
var tapeNock = require('tape-nock');
// call tapeNock with tape and an options object
var test = tapeNock(tape, {
fixtures: path.join(__dirname, 'fixtures')
});
describe('Version test', function() {
this.timeout(0);
it('test version', function(done) {
test('record_version.json', function(t) {
request(app)
.get('/hammer/version')
.expect(200, {
url: "http://httpbin.org/get",
version: '0.1.0'
})
.end(function(err, res) {
if (err) return done(err);
t.end();
done();
});
});
});
});
" package.json":
{
"name": "remote_node_test",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"express": "^4.14.0",
"mocha": "^3.2.0",
"nock": "^9.0.2",
"superagent": "^3.3.1",
"supertest": "^2.0.1",
"tape": "^4.6.3",
"tape-nock": "^1.4.0"
},
"devDependencies": {
"mocha": "^3.2.0"
},
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC"
}
测试使用" mocha":
运行NOCK_BACK_MODE=record node_modules/mocha/bin/mocha
首次运行,第二次运行"锁定/记录"不起作用。
错误:
% NOCK_BACK_MODE=lockdown node_modules/mocha/bin/mocha test.js :(
Version test
TAP version 13
# details.json
1) return current version
0 passing (32ms)
1 failing
1) Version test return current version:
TypeError: Cannot read property 'status' of undefined
at Test._assertStatus (node_modules/supertest/lib/test.js:263:10)
at Test._assertFunction (node_modules/supertest/lib/test.js:281:11)
at Test.assert (node_modules/supertest/lib/test.js:171:18)
at Server.assert (node_modules/supertest/lib/test.js:131:12)
at emitCloseNT (net.js:1553:8)
at _combinedTickCallback (internal/process/next_tick.js:71:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
记录的是所有请求,但我只需要记录"外部"请求,并防止"模拟/录制"我的内在逻辑。
答案 0 :(得分:2)
如果你正在使用mocha,你可能想要找一个类似mocha特定的nock / nockBack助手(https://www.npmjs.com/search?q=mocha+nock)
话虽这么说,你也可能遇到问题,其中HTTP调用supertest对应用程序的响应被nockBack选中。
我做了一个小例子,只使用磁带做你想要完成的事情: https://github.com/Flet/tape-nock-with-supertest-example
在setup-tape-nock.js中定义的afterRecord和之前的函数可能是你需要的秘密酱,即使使用其他一些nockBack mocha助手也是如此。
希望这有帮助!
答案 1 :(得分:0)
一个解决方案似乎是“replay”,并将请求的“passThrough”配置到我的本地应用。
/* global describe, it */
'use strict';
const request = require('supertest');
const app = require('./app.js');
var path = require('path');
const Replay = require('replay');
Replay.fixtures = __dirname + '/fixtures/replay';
Replay.passThrough('localhost', '127.0.0.1', '0.0.0.0');
describe('Version test', function() {
this.timeout(0);
it('test version', function(done) {
request(app)
.get('/hammer/version')
.expect(200, {
url: "http://httpbin.org/get",
version: '0.1.0'
})
.end(function(err, res) {
if (err) return done(err);
done();
});
});
});