【问题标题】:node.js: Mock http request and responsenode.js:模拟 http 请求和响应
【发布时间】:2011-11-05 17:36:00
【问题描述】:

是否有方便的方法来模拟用于单元测试中间件的 HTTP 请求和响应对象?

【问题讨论】:

  • 您是否有理由不在测试用例中包含真实的 HTTP 请求?在异步测试设置中应该是可行的。
  • 我同意了,但老实说,它不像单元测试,更像是集成测试。无论如何,到目前为止,这是我最好的选择。
  • 我不想包含真正的 http 请求,因为它使我的测试速度变慢,可靠性降低。例如,如果我调用的服务之一是由数据库支持的,并且由于 QA 中的数据库已被清除(或与 prod 同步或其他)而失败,那么我的测试开始失败。此外,如果您不模拟,则几乎不可能可靠地强制某些错误情况发生。不,我喜欢模拟——它们使测试更容易、更可靠。

标签: node.js mocking express


【解决方案1】:

看起来https://github.com/howardabrams/node-mocks-httphttps://github.com/vojtajina/node-mocks 都可以用来创建模拟http.ServerRequesthttp.ServerResponse 对象。

【讨论】:

  • node-mocks-http 看起来是原始帖子的一个很好的解决方案。
  • 好的,刚刚遇到这个,node-mocks-http 很棒。 (至于测试请求处理程序而不实际发出 http 请求)
【解决方案2】:

从标签看来,这个问题是关于 Express 的。那样的话supertest就很好了:

var request = require('supertest')
  , express = require('express');

var app = express();

app.get('/user', function(req, res){
  res.send(201, { name: 'tobi' });
});

request(app)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '20')
  .expect(201)
  .end(function(err, res){
    if (err) throw err;
  });

对于一般 Node 使用,Flatiron Nock 看起来是个不错的选择:

var nock = require('nock');
var example = nock('http://example.com')
                .get('/foo')
                .reply(200, { foo: 'bar' });

var http = require('http');
var options = {
  host: 'example.com',
  port: 80,
  path: '/foo',
  method: 'GET'
}
var req = http.request(options, function(res) {
  res.on('data', function(chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('error: ' + e);
});

req.end();

输出:

主体:{"foo":"bar"}

【讨论】:

【解决方案3】:

我正在使用 nodejutsu 模拟:

https://github.com/nodejitsu/mock-request

也许这就是你要找的。​​p>

【讨论】:

【解决方案4】:

我编写了一个库来模拟通过标准 HTTP 或请求模型发出的请求的响应:

https://github.com/ctide/fakeweb

【讨论】:

    【解决方案5】:

    查看https://github.com/timsavery/node-hmocknpm install hmock...欢迎任何反馈!到目前为止,该解决方案对我来说效果很好。

    【讨论】:

      【解决方案6】:

      Mockery 看起来很棒。

      本质上,它会劫持require 调用,并返回您指定的不同对象/函数存根。

      【讨论】:

        【解决方案7】:

        迟来的,如果您只想对处理程序进行单元测试,您可以将 express.Requestexpress.Response 对象注入请求函数,或者在本例中,注入您的中间件。 对我来说,一个似乎提供最少方法同时保持简单的方法是@jest-mock/express

        如果您使用的是supertestnock,那么您正在执行一个可以耦合多个测试的集成测试。我还会研究它在内部是如何工作的,因为一旦它停止工作,调试起来会很痛苦。

        it('should call next',
                async () => {
                    const req = getMockReq({
                      headers: {
                        'user-agent': 'Chrome',
                      },
                      path: '/path',
                    })
                    const{res, next, clearMockRes} = getMockRes({})
                    await middleware(req, res, next)
        
                    expect(res.send).not.toHaveBeenCalledWith()
                    expect(next).toHaveBeenCalled()
                })
        

        【讨论】:

          【解决方案8】:

          我鼓励您使用motty。为什么我们需要另一个代码?

          【讨论】:

            猜你喜欢
            • 2018-01-05
            • 2020-03-24
            • 2013-03-23
            • 1970-01-01
            • 2014-08-15
            • 1970-01-01
            • 2018-04-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多