Javascript 验证中间件未调用存根-节点JS,sinon

Javascript 验证中间件未调用存根-节点JS,sinon,javascript,node.js,unit-testing,sinon,Javascript,Node.js,Unit Testing,Sinon,以前也有人问过类似的问题,我也看过,也跟着问过,但运气不好: 我从中得到的一般答案是,在auth中间件方法被存根后,应该需要模块(app.js)。我已经这样做了,但仍然调用原始中间件: src/app.js const authentication = require('./authentication') ... app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed]) module.expo

以前也有人问过类似的问题,我也看过,也跟着问过,但运气不好:

  • 我从中得到的一般答案是,在auth中间件方法被存根后,应该需要模块(
    app.js
    )。我已经这样做了,但仍然调用原始中间件:

    src/app.js

    const authentication = require('./authentication')
    
    ...
    
    app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
    
    module.exports = app
    
    const { isValidAuth } = require('./lib')
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    // 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: {
            ...
        }
    }
    
    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()
        })
    })
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    src/authentication.js

    const authentication = require('./authentication')
    
    ...
    
    app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
    
    module.exports = app
    
    const { isValidAuth } = require('./lib')
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    // 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: {
            ...
        }
    }
    
    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()
        })
    })
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    \uuuuuuuuuuuuuuuuuuuuuuuuuuuu帮助程序/supertest.js

    const authentication = require('./authentication')
    
    ...
    
    app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
    
    module.exports = app
    
    const { isValidAuth } = require('./lib')
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    // 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: {
            ...
        }
    }
    
    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()
        })
    })
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    users.spec.js

    const authentication = require('./authentication')
    
    ...
    
    app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
    
    module.exports = app
    
    const { isValidAuth } = require('./lib')
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    // 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: {
            ...
        }
    }
    
    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()
        })
    })
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    \uuuu存根\uuuu/authentication.js

    const authentication = require('./authentication')
    
    ...
    
    app.use(['/api/users', '/api/groups'], authentication.ensureAuthenticed])
    
    module.exports = app
    
    const { isValidAuth } = require('./lib')
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    // 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: {
            ...
        }
    }
    
    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()
        })
    })
    
    exports.ensureAuthenticated = (req, res, next) => {
        ...
    }
    
    在users.spec.js中,我在方法被模拟后加载supertest.js(在src/app.js中加载),因此我不确定为什么仍在调用原始方法


    我还尝试在模拟之前手动清除缓存,但仍然不起作用。

    我认为解决方案将使用Supertest代替(或与Supertest一起)。
    通过“重新布线”,可以模拟模块的顶级组件。尽管您需要在传递到Supertest之前模拟中间件。

    我认为解决方案将使用Supertest来代替(或与之一起使用)。
    通过“重新布线”,可以模拟模块的顶级组件。虽然在传递给Supertest之前需要模拟中间件。

    结果表明,这与Supertest.js需要app.js有关。我现在有users.spec.js需要该应用程序,并将其作为参数传递到supertest方法中。它现在起作用了。仍然不确定为什么

    发现这与supertest.js需要app.js有关。我现在有users.spec.js需要该应用程序,并将其作为参数传递到supertest方法中。它现在起作用了。仍然不确定为什么,虽然是个好主意,但不幸的是,这个主意不管用。好主意,但不幸的是,这个主意也不管用