TypeError:将sinon stud用于中间件时,next不是函数

时间:2020-10-17 15:11:13

标签: node.js mocha ejs chai sinon

我正在尝试存根我的身份验证中间件,但是它总是抛出错误

TypeError: next is not a function

测试文件中的代码

var auth;
var app;
beforeEach(function() {
  auth = require('../middleware/auth');
  
  sinon.stub(auth, 'isAuthenticated')
    .callsFake((req, res, next) => next());
    app = require('../app')
});

afterEach(function() {
  // restore original method
  auth.isAuthenticated.restore();
});


describe('Employee Controller - Test Async Functions', function () {
  it('should access position list', function (done) {

    sinon.stub(Position, 'find');
    Position.find.returns(positions);

    request(app)
      .get('/positions/')
      .then(function (response) {
        expect(response.status).to.equals(401);
        done();
      });
      Position.find.restore();
  });
});

这是我在app.js中的路由器代码

app.use('/positions', authMiddleWare.isAuthenticated(enums.roles.Employee), employeeRouter);

完整的错误代码。

1) "before each" hook for "should access position list":
     TypeError: next is not a function
      at Object.<anonymous> (test\employee-controller.test.js:43:36)
      at Object.invoke (node_modules\sinon\lib\sinon\behavior.js:163:32)
      at Object.functionStub (node_modules\sinon\lib\sinon\stub.js:39:43)
      at Function.invoke (node_modules\sinon\lib\sinon\proxy-invoke.js:47:47)
      at Object.isAuthenticated (node_modules\sinon\lib\sinon\proxy.js:216:26)
      at Object.<anonymous> (app.js:47:36)
      at Module._compile (internal/modules/cjs/loader.js:1137:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
      at Module.load (internal/modules/cjs/loader.js:985:32)
      at Function.Module._load (internal/modules/cjs/loader.js:878:14)
      at Module.require (internal/modules/cjs/loader.js:1025:19)
      at require (internal/modules/cjs/helpers.js:72:18)
      at Context.<anonymous> (test\employee-controller.test.js:44:11)
      at processImmediate (internal/timers.js:456:21)

0 个答案:

没有答案