最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

不能嘲笑护照进行身份验证(“本地”)方法兴农

IT培训 admin 4浏览 0评论

不能嘲笑护照进行身份验证(“本地”)方法兴农

我试图嘲弄passport.authenticate(“本地”):

 app.post('/login', passport.authenticate('local'), (req, res, next) => {console.log('should enter');})

现在用兴农,但该方法不执行登录路由内的console.log

beforeEach(function (done) {
      aut = sinon.stub(server.passport, 'authenticate').returns(() => {});
      server.app.on('appStarted', function () {
        done();
      });
    });

afterEach(() => {
  aut.restore();
});

describe('Login', () => {
  it('should login', (done) => {
    chai.request(server.app)
      .post('/login')
      .send({
        username: 'user',
        password: 'pas'
      })
      .end(function (error, response, body) {
        return done();
      });
  });
});

此外,当我把真实的passport.authenticate(“本地”)内部的模拟像这样:

app.post('/login', () => {}, (req, res, next) => {console.log('should enter');})

它仍然没有进入,这意味着兴农callFake不会在所有帮助的途径。只有当我删除

passport.authenticate( '本地')

从/登录路由将“应该登录”测试进入的路线。

在beforeEach实施兴农

let server = require('../../../app.js');
let expect = chai.expect;
chai.use(chaiHttp);

var aut;
beforeEach(() => {
  aut = sinon.stub(server.passport, 'authenticate').returns((req, res, next) => next());
});

app.js

const app = express();

middleware.initMiddleware(app, passport);

const dbName = 'dbname';
const connectionString = 'connect string';

mongo.mongoConnect(connectionString).then(() => {
        console.log('Database connection successful');
        app.listen(5000, () => console.log('Server started on port 5000'));
    })
    .catch(err => {
        console.error('App starting error:', err.stack);
        process.exit(1);
    });

// If the Node process ends, close the Mongoose connection
process.on('SIGINT', mongo.mongoDisconnect).on('SIGTERM', mongo.mongoDisconnect);

register.initnulth(app);

login.initfirst(app, passport);
logout.initsecond(app);


module.exports = app;
回答如下:

看来你想使用中间件回调,什么也不做,但只是让请求通过后中间件处理。这样的回调将是:

(req, res, next) => next()

中间件必须调用next()的要求继续被后来中间件处理。所以,你应该设置你的存根是这样的:

aut = sinon.stub(server.passport, 'authenticate').returns((req, res, next) => next());

不能嘲笑护照进行身份验证(“本地”)方法兴农

我试图嘲弄passport.authenticate(“本地”):

 app.post('/login', passport.authenticate('local'), (req, res, next) => {console.log('should enter');})

现在用兴农,但该方法不执行登录路由内的console.log

beforeEach(function (done) {
      aut = sinon.stub(server.passport, 'authenticate').returns(() => {});
      server.app.on('appStarted', function () {
        done();
      });
    });

afterEach(() => {
  aut.restore();
});

describe('Login', () => {
  it('should login', (done) => {
    chai.request(server.app)
      .post('/login')
      .send({
        username: 'user',
        password: 'pas'
      })
      .end(function (error, response, body) {
        return done();
      });
  });
});

此外,当我把真实的passport.authenticate(“本地”)内部的模拟像这样:

app.post('/login', () => {}, (req, res, next) => {console.log('should enter');})

它仍然没有进入,这意味着兴农callFake不会在所有帮助的途径。只有当我删除

passport.authenticate( '本地')

从/登录路由将“应该登录”测试进入的路线。

在beforeEach实施兴农

let server = require('../../../app.js');
let expect = chai.expect;
chai.use(chaiHttp);

var aut;
beforeEach(() => {
  aut = sinon.stub(server.passport, 'authenticate').returns((req, res, next) => next());
});

app.js

const app = express();

middleware.initMiddleware(app, passport);

const dbName = 'dbname';
const connectionString = 'connect string';

mongo.mongoConnect(connectionString).then(() => {
        console.log('Database connection successful');
        app.listen(5000, () => console.log('Server started on port 5000'));
    })
    .catch(err => {
        console.error('App starting error:', err.stack);
        process.exit(1);
    });

// If the Node process ends, close the Mongoose connection
process.on('SIGINT', mongo.mongoDisconnect).on('SIGTERM', mongo.mongoDisconnect);

register.initnulth(app);

login.initfirst(app, passport);
logout.initsecond(app);


module.exports = app;
回答如下:

看来你想使用中间件回调,什么也不做,但只是让请求通过后中间件处理。这样的回调将是:

(req, res, next) => next()

中间件必须调用next()的要求继续被后来中间件处理。所以,你应该设置你的存根是这样的:

aut = sinon.stub(server.passport, 'authenticate').returns((req, res, next) => next());
发布评论

评论列表 (0)

  1. 暂无评论