我正在尝试嘲笑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();
});
});
});
另外, 当我将模拟物放入真实的 passport.authenticate('local')中时,如下所示:
app.post('/login', () => {}, (req, res, next) => {console.log('should enter');})
它仍然没有输入路线,这意味着sinon callFake 完全没有帮助。仅当我删除
passport.authenticate('local')
在 / login 路由中将进行“应登录”测试输入路由。
在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;
答案 0 :(得分:2)
似乎您要使用一个中间件回调,该回调什么都不做,只是让请求由以后的中间件处理。这样的回调将是:
(req, res, next) => next()
中间件必须调用next()
,以使请求继续被以后的中间件处理。因此,您应该像这样设置存根:
aut = sinon.stub(server.passport, 'authenticate').returns((req, res, next) => next());