您可以使用sinon
to stubisAuthenticated
方法,但是应该auth.isAuthenticated
在将对的引用设置为中间件之前进行此操作,因此在创建index.js
and 之前需要这样做app
。您最有可能希望将其beforeEach
挂钩:
var app;
var auth;
beforeEach(function() {
auth = require('../wherever/auth/auth.service');
sinon.stub(auth, 'isAuthenticated')
.callsFake(function(req, res, next) {
return next();
});
// after you can create app:
app = require('../../wherever/index');
});
afterEach(function() {
// restore original method
auth.isAuthenticated.restore();
});
it('it should return a 200 response', function(done) {
request(app).post('/subscriptions/sync')
.set('Authorization','Bearer '+ authToken)
.send({receipt: newSubscriptionReceipt })
.expect(200,done);
});
请注意,即使在auth.isAuthenticated
还原后,现有app
实例也将具有存根作为中间件,因此,app
如果由于某种原因需要获得原始行为,则需要创建另一个实例。