无法使用 sinon 模拟护照身份验证('local')方法
Cannot mock passport authenticate('local') method with sinon
我正在尝试模拟 passport.authenticate('local'):
app.post('/login', passport.authenticate('local'), (req, res, next) => {console.log('should enter');})
我正在使用 Sinon,但该方法不会在登录路由中执行 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();
});
});
});
此外,
当我将 mock 放入真实的 passport.authenticate('local') 时,如下所示:
app.post('/login', () => {}, (req, res, next) => {console.log('should enter');})
它仍然没有进入路由,这意味着 sinon callFake 根本没有帮助。只有当我删除
passport.authenticate('local')
从 /login 路由 'should login' test 进入路由。
在beforeEach中实现sinon
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('local'):
app.post('/login', passport.authenticate('local'), (req, res, next) => {console.log('should enter');})
我正在使用 Sinon,但该方法不会在登录路由中执行 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();
});
});
});
此外, 当我将 mock 放入真实的 passport.authenticate('local') 时,如下所示:
app.post('/login', () => {}, (req, res, next) => {console.log('should enter');})
它仍然没有进入路由,这意味着 sinon callFake 根本没有帮助。只有当我删除
passport.authenticate('local')
从 /login 路由 'should login' test 进入路由。
在beforeEach中实现sinon
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());