Auth 中间件不调用存根 - NodeJS,sinon
Auth middleware not calling stub - NodeJS, sinon
之前也有人问过类似的问题,我看过并关注了他们,但没有成功:
- node express es6 sinon stubbing middleware not working
我从这些得到的一般答案是模块(app.js
在我的例子中)应该是必需的 在 auth 中间件方法被存根之后。我这样做了,但是还是调用原来的中间件:
src/app.js
const authentication = require('./authentication')
...
app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
module.exports = app
src/authentication.js
const { isValidAuth } = require('./lib')
exports.ensureAuthenticated = (req, res, next) => {
...
}
__helpers__/supertest.js
// This file just calls endpoints with supertest but this is the only file
// that includes 'app'
const app = require('../../src/app')
module.exports = {
post: {
...
},
get: {
...
},
put: {
...
},
delete: {
...
}
}
users.spec.js
const authentication = require('../../src/authentication')
const authenticationStubs = require('../__stubs__/authentication')
let supertest
let ensureAuthStub
describe('Users', () => {
before(() => {
ensureAuthStub = sinon.stub(authentication, 'ensureAuthenticated').callsFake(authenticationStubs.ensureAuthenticated)
supertest = require('../__helpers__/supertest')
})
// tests
after(() => {
ensureAuthStub.restore()
})
})
__stubs__/authentication.js
exports.ensureAuthenticated = (req, res, next) => {
...
}
在 users.spec.js 中,我加载 supertest.js(加载在 src/app.js) AFTER 该方法已被模拟,所以我不确定为什么仍在调用原始方法。
我也尝试过在模拟之前手动清除缓存,但仍然不起作用。
我认为解决方案是使用 Rewire 代替(或与)Supertest。
Rewire 允许您模拟模块的顶级组件。尽管在传递给 Supertest 之前您需要模拟中间件。
原来这与 supertest.js 需要 app.js 有关。我现在有 users.spec.js 需要应用程序并将其作为参数传递到超级测试方法中。现在可以了。仍然不确定为什么
之前也有人问过类似的问题,我看过并关注了他们,但没有成功:
- node express es6 sinon stubbing middleware not working
我从这些得到的一般答案是模块(app.js
在我的例子中)应该是必需的 在 auth 中间件方法被存根之后。我这样做了,但是还是调用原来的中间件:
src/app.js
const authentication = require('./authentication')
...
app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
module.exports = app
src/authentication.js
const { isValidAuth } = require('./lib')
exports.ensureAuthenticated = (req, res, next) => {
...
}
__helpers__/supertest.js
// This file just calls endpoints with supertest but this is the only file
// that includes 'app'
const app = require('../../src/app')
module.exports = {
post: {
...
},
get: {
...
},
put: {
...
},
delete: {
...
}
}
users.spec.js
const authentication = require('../../src/authentication')
const authenticationStubs = require('../__stubs__/authentication')
let supertest
let ensureAuthStub
describe('Users', () => {
before(() => {
ensureAuthStub = sinon.stub(authentication, 'ensureAuthenticated').callsFake(authenticationStubs.ensureAuthenticated)
supertest = require('../__helpers__/supertest')
})
// tests
after(() => {
ensureAuthStub.restore()
})
})
__stubs__/authentication.js
exports.ensureAuthenticated = (req, res, next) => {
...
}
在 users.spec.js 中,我加载 supertest.js(加载在 src/app.js) AFTER 该方法已被模拟,所以我不确定为什么仍在调用原始方法。
我也尝试过在模拟之前手动清除缓存,但仍然不起作用。
我认为解决方案是使用 Rewire 代替(或与)Supertest。 Rewire 允许您模拟模块的顶级组件。尽管在传递给 Supertest 之前您需要模拟中间件。
原来这与 supertest.js 需要 app.js 有关。我现在有 users.spec.js 需要应用程序并将其作为参数传递到超级测试方法中。现在可以了。仍然不确定为什么