如何使用sinon和mocha在node js中模拟可配置中间件 [英] how to mock configurable middleware in node js with sinon and mocha

查看:147
本文介绍了如何使用sinon和mocha在node js中模拟可配置中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可配置的中间件,可以在其中传递参数并基于该中间件调用下一个函数。

I have configurable middleware where I can pass parameters and based on that it calls next function.

中间件代码:

文件:my-middleware.js

File: my-middleware.js

exports.authUser = function (options) {
  return function (req, res, next) {
    // Implement the middleware function based on the options object
    next()
  }
}

var mw = require('./my-middleware.js')

app.use(mw.authUser({ option1: '1', option2: '2' }))

如何使用sinon js模拟中间件?

How to mock the middleware using sinon js?

我已经这样做了,但是它使我抛出 TypeError:next不是一个函数。

I have done in this way but, it throwing me "TypeError: next is not a function".

这是我的单元测试代码:

Here is my unit test code:

  it("Should return data by id", (done: any) => {

        sandbox.stub(mw, 'authUser')
            .callsFake((req: any, res: any, next: any) => { return next(); });

        server = require('../../index');

        let req = {
            "id": '123'
        }

        chai.request(server)
            .post("/user")
            .send(req)
            .end((request, res) => {

                expect(res.status).to.equal(200);
                expect(res.body.success).to.equal(true);

                done();
            });
    });

您能帮我模拟可配置的中间件吗?

Can you help me to mock the configurable middleware? Thanks in advance!!

推荐答案

authUser 是一个高级函数,因此您需要为此存根。

The authUser is a high order function, so you need to stub it for this.

例如

mws.js

exports.authUser = function(options) {
  return function(req, res, next) {
    // Implement the middleware function based on the options object
    next();
  };
};

app.js

const express = require('express');
const mws = require('./mws');
const app = express();

app.use(mws.authUser({ option1: '1', option2: '2' }));

app.post('/user', (req, res, next) => {
  res.json({ success: true });
});

module.exports = app;

app.integration.test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const sandbox = require('sinon').createSandbox();
const mws = require('./mws');

chai.use(chaiHttp);
const expect = chai.expect;

describe('61818474', () => {
  afterEach(() => {
    sandbox.restore();
  });
  it('Should return data by id', (done) => {
    sandbox.stub(mws, 'authUser').callsFake((options) => (req, res, next) => {
      return next();
    });
    const server = require('./app');
    const req = { id: '123' };
    chai
      .request(server)
      .post('/user')
      .send(req)
      .end((request, res) => {
        expect(res.status).to.equal(200);
        expect(res.body.success).to.equal(true);
        sandbox.assert.calledWith(mws.authUser, { option1: '1', option2: '2' });
        done();
      });
  });
});

包含覆盖率报告的集成测试结果:

integration test results with coverage report:

  61818474
    ✓ Should return data by id (257ms)


  1 passing (265ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      80 |      100 |   33.33 |      80 |                   
 app.js   |     100 |      100 |     100 |     100 |                   
 mws.js   |   33.33 |      100 |       0 |   33.33 | 2-4               
----------|---------|----------|---------|---------|-------------------

这篇关于如何使用sinon和mocha在node js中模拟可配置中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆