javascript - 在 nodejs express 应用程序中记录所有远程调用以进行测试

标签 javascript node.js express testing supertest

目标是重新编写 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)

记录了所有请求,但我只需要记录“外部”请求,并防止“模拟/记录”我的内部逻辑。

最佳答案

如果您使用的是 mocha,您可能需要寻找类似的 nock/nockBack 帮助程序,它们是特定于 mocha 的 (https://www.npmjs.com/search?q=mocha+nock)

话虽如此,您也可能会遇到 supertest 对应用程序进行的 HTTP 调用被 nockBack 获取的问题。

我做了一个小例子,它只使用磁带来完成你想要完成的事情: https://github.com/Flet/tape-nock-with-supertest-example

setup-tape-nock.js 中定义的 afterRecord 和 before 函数可能是您需要的秘诀,即使使用其他一些 nockBack mocha 助手也是如此。

希望这对您有所帮助!

关于javascript - 在 nodejs express 应用程序中记录所有远程调用以进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41246426/

相关文章:

javascript - Firefox 插件 SDK : prompt with multiple checkboxes

javascript - 为什么 ARM 芯片有一条指令名称中带有 Javascript 的指令(FJCVTZS)?

node.js - 在 Heroku 上运行 Node.js 应用程序时出错

postgresql - 我在哪里可以指定编译的迁移

javascript - 谷歌地图api获取 key "Accept requests from these HTTP referrers"

node.js - 清晰的手动模拟 Jest

javascript - 是否可以在javascript的函数中定义一个全局常量

node.js - 通过 Mongoose 模式传递硬编码值

node.js - 单一资源粒度的 Express acl

php - Backbone +RequireJS : HTML files loaded with RequireJS are interpreted as JS files