简体   繁体   English

使用Sinon存根和还原auth函数仍会导致使用存根进行Mocha测试

[英]Stubbing and restoring auth function with Sinon still results in Mocha test using stub

We are trying to stub out an authentication middleware in an express app in some but not all of our tests, and we are having trouble making stubbing work for us. 我们正在某些而非全部测试中尝试在Express应用程序中存入身份验证中间件,并且在使存根对我们有效方面遇到了麻烦。

Our mocha test looks something like this: describe('primaryDeal routes unit test', () => { 我们的摩卡测试看起来是这样的:describe('primaryDeal Routes unit test',()=> {

    describe('Authentication allows for data fetching', () => {
        let app;
        let getPrimaryDealData;
        let queryParams;
        let isAuthenticated;    
        let count = 0;

        beforeEach(() => {
            // console.log(isAuthenticated);
            if (count === 0) {
                isAuthenticated = sinon.stub(authCheck, 'isAuthenticated');
                isAuthenticated.callsArg(2);
            }
            app = require('../../lib/index.js');
        });   


        afterEach(() => {
            if (count === 0) {
                isAuthenticated.restore();
            }
            app.close();
            count++;
        });

        it(('should send an API request, validate input and return 200 response'), () => {
            return chai.request(app)
                .get('/api/contracts/')
                .then((res) => {
                    expect(res).to.have.status(200);
                });
        });

        it(('should respond with forbidden'), () => {    

            app = require('../../lib/index.js');    

            return chai.request(app)
                .get('/api/contracts/')
                .catch((res, err) => {
                    expect(res).to.have.status(403);
                });
        });
    });    
});

Our stub works as intended for the first it , but the stub appears to not be restored for the second it and our authentication middleware is not being run. 我们存根按预期工作的第一it ,但短线似乎无法恢复第二it和我们的认证中间件没有运行。 Both tests work if the other is commented out. 如果另一项被注释掉,则这两种测试均有效。

We've tried separating these blocks in different files, and in different describe blocks, we've also tried switching the order of the it blocks, and we've tried giving both chai.request(app) separate servers but we are at a loss. 我们尝试将这些块分隔在不同的文件中,并在不同的describe块中,我们还尝试切换it块的顺序,并尝试将chai.request(app)都提供给单独的服务器,但我们位于失利。

Why could it be that our second it statement isn't calling our Auth middleware? 为什么我们的第二个it语句没有调用我们的Auth中间件?

I recommend you to use sandbox. 我建议您使用沙箱。 It's more elegant to use. 使用起来更优雅。 No need to restore stubs individually. 无需单独还原存根。 Here is an sample: 这是一个示例:

 let sandbox; beforeEach(() => { sandbox = sinon.sandbox.create(); isAuthenticated = sandbox.stub(authCheck, 'isAuthenticated'); }); afterEach(() => { sandbox.restore(); }); 

Also, could you try to add replace 另外,您是否可以尝试添加替换

 app = require('../../lib/index.js'); 
to

 delete require.cache[require.resolve('../../lib/index.js')] app = require('../../lib/index.js'); 

Another thought, maybe you need to use reset not restore in this particular case? 再想一想,也许您需要在这种特殊情况下使用resetrestore

PS It's also good to see index.js source as well PS也很高兴看到index.js源代码

I had the same issue and tried this solution without success. 我遇到了同样的问题,并尝试了此解决方案,但没有成功。 However the delete require.cache[require.resolve('../../lib/index.js')] gave me an idea. 但是删除require.cache [require.resolve('../../ lib / index.js')]给了我一个主意。 I was able to use decache instead of delete require. 我能够使用decache而不是delete require。 This resolved the problem for me. 这为我解决了问题。

const decache = require('decache');


decache('../../lib/index.js');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM