存根中间件 [英] Stubbing Out Middleware

查看:85
本文介绍了存根中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sinon在快速路由中存入一些自定义中间件,但是它没有按我期望的那样工作.我希望它不会记录我正在验证...",而是记录存根"到控制台.看起来sinon没有正确地存根中间件.

I'm trying to use Sinon to stub out some custom middleware in an express route but it's not working as I expect it to. I expect it to not log "I am authenticating..." and instead log "Stubbed" to the console. It looks like sinon is not stubbing out the middleware correctly.

test/test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');
chai.use(chaiHttp);
const should = chai.should();
const auth = require('../auth');

const app = require('../app')

describe('My routes', function() {
    let checkTokenStub;
    beforeEach(()=>{
        checkTokenStub = sinon.stub(auth,'checkToken').callsFake(()=>{
            console.log('Stubbed');
        });;
    })
     it('returns hello', function(done) {
            chai.request(app)
                .get('/')
                .set('X-Auth-Token', 'xyz123')
                .end((err,res)=>{
                    res.text.should.be.eql('Hello')

                    done(err)
                })
        });
    });

app.js

var express = require('express'),
    app = express();
var router = express.Router();
app.use('/', require('./router'));

module.exports = app;

auth.js

exports.checkToken = function(req, res, next) {

    console.log('I am authenticating...')

    var authToken = req.get('x-auth-token');

    if (!authToken)
        return res.sendStatus(401);

    next();
}

router.js

var express = require('express'),
router = express.Router();
auth = require('./auth');

router.get('/', auth.checkToken, function(req, res, next) {
    return res.send('Hello');
});

module.exports = router;

package.json

{
  "name": "sinontest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha --watch"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "chai": "^4.1.2",
    "chai-http": "^3.0.0",
    "mocha": "^4.0.1",
    "sinon": "^4.1.1"
  },
  "dependencies": {
    "express": "^4.16.4"
  }
}

推荐答案

@ Gonzalo.-在评论中回答了这个问题.我必须将应用程序的需求移到存根之后.

@Gonzalo.- answered this question in the comments. I had to move the requiring of app to after the stub.

test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');
chai.use(chaiHttp);
const should = chai.should();
const auth = require('../auth');
let app;


describe('My routes', function() {
    let checkTokenStub;
    before(()=>{
        checkTokenStub = sinon.stub(auth,'checkToken').callsFake((req,res,next)=>{
            console.log('Stubbed');
            next()
        });

        app = require('../app')

    })
     it('returns hello', function(done) {
            chai.request(app)
                .get('/')
                .set('X-Auth-Token', 'xyz123')
                .end((err,res)=>{
                    res.text.should.be.eql('Hello')

                    done(err)
                })
        });
    });

这篇关于存根中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆