簡體   English   中英

將所有遠程呼叫記錄在nodejs express應用中以進行測試

[英]Record all remote calls in a nodejs express app for testing

我們的目標是進行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)

記錄的是所有請求,但我只需要記錄“外部”請求,並防止“模擬/記錄”我的內部邏輯。

如果您使用的是摩卡咖啡,則可能需要尋找特定於摩卡咖啡的類似nock / nockBack幫助器( https://www.npmjs.com/search?q=mocha+nock

話雖這么說,您也可能會遇到問題,其中nockBack會對應用程序進行HTTP調用超級測試。

我舉了一個小例子,僅使用磁帶來完成您要完成的任務: https : //github.com/Flet/tape-nock-with-supertest-example

即使使用其他nockBack摩卡助手,setup-tape-nock.js中定義的afterRecord和before函數也可能是您需要的秘訣。

希望這可以幫助!

一種解決方案似乎是“ 重播 ”並配置對本地應用程序的請求的“ 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();
            });
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM