【问题标题】:Record all remote calls in a nodejs express app for testing在 nodejs express 应用程序中记录所有远程调用以进行测试
【发布时间】:2016-12-20 15:59:35
【问题描述】:

目标是记录 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)

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

【问题讨论】:

    标签: javascript node.js express testing supertest


    【解决方案1】:

    如果您使用的是 mocha,您可能需要寻找类似的 mocha 特有的 nock/nockBack 助手 (https://www.npmjs.com/search?q=mocha+nock)

    话虽如此,您也可能会遇到问题,即超测试对应用程序的 HTTP 调用被 nockBack 拾取。

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

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

    希望这会有所帮助!

    【讨论】:

    【解决方案2】:

    一个解决方案似乎是“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();
                });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2012-02-05
      • 1970-01-01
      • 2017-10-18
      • 2013-11-21
      • 2018-03-08
      相关资源
      最近更新 更多