如何在一个restify HTTP处理程序中测试我的异步代码?

时间:2017-02-01 14:08:17

标签: node.js sequelize.js sinon chai restify

我的Restify项目中有一个处理HTTP GET请求的函数。经过一些处理后,它使用Sequelize查找当前会话的用户实体。 User.findOne函数返回一个promise,根据该promise的结果,我发送一个200或404的HTTP响应。

static getMe(req, res, next) {
    const userInfo = BaseController.getUserSession(req);

    // hard to test this part
    User.findOne({
      where: {email: userInfo.email}
    }).then(function(user) {
      if (user) BaseController.respondWith200(res, user);
      else BaseController.respondWith404(res, 'User not found.');
    }, function(error) {
      BaseController.respondWith404(res, error);
    }).then(function() {
      return next();
    });
}

我已经尝试了几个不同的库来帮助测试,所以如果这是一个混乱的事情,我很抱歉。这是我的beforeEach函数,用于我的测试:

const usersFixture = [
  {id:2, email:'ozzy@osbourne.com', facebookId:54321, displayName: 'Ozzy Osbourne'},
  {id:3, email:'zakk@wylde.com', facebookId:34521, displayName: 'Zakk Wylde'},
  {id:4, email:'john@lennon.com', facebookId:12453, displayName: 'John Lennon'}
];

this.findOneSpy = sinon.spy(function(queryObj) {
  return new Promise(function(resolve, reject) {
    const user = usersFixture.find(function(el) { return el.email === queryObj.where.email });
    if (user) resolve(user);
    else resolve(null);
  });
});

this.respondWith200Spy = sinon.spy(function(res, data) {});
this.respondWith400Spy = sinon.spy(function(res, error) {});
this.respondWith404Spy = sinon.spy(function(res, error) {});

this.controller = proxyquire('../../controllers/user-controller', {
  '../models/user': {
    findOne: this.findOneSpy
  },
  './base-controller': {
    respondWith200: this.respondWith200Spy,
    respondWith400: this.respondWith400Spy,
    respondWith404: this.respondWith404Spy
  }
});

以下是我的测试之一:

it('should return 200 with user data if user email matches existing user', function() {
  // THIS FUNCTION IS NEVER HIT
  this.respondWith200Spy = function(res, data) {
    data.should.equal({id:4, email:'john@lennon.com', facebookId:12453, displayName: 'John Lennon'});
    done();
  };
  const req = {session:{user:{email:'john@lennon.com'}}};
  this.controller.getMe(req, this.res, this.nextSpy);
  this.findOneSpy.should.have.been.called;
});

由于我们实际上并没有将回调函数传递给函数,并且函数没有真正返回任何东西(只是在其他地方做异步),我无法弄清楚如何测试它以确保它正常工作。任何帮助表示赞赏。

实际代码工作得很好。我只是想在项目中进行一些高质量的单元测试。谢谢!

1 个答案:

答案 0 :(得分:0)

我最终找到了使用proxyquire的方法。我刚刚重新编写了我正在测试的控制器类,并使respondWith200回调作出断言。然后我为next函数创建了一个新的间谍,它只调用done(传递给测试用例)。我确认代码已全部受到攻击。

it('should return 200 with user data if user email matches existing user', function(done) {
  const controller = proxyquire('../../controllers/user-controller', {
    '../models/user': {
      findOne: this.findOneSpy
    },
    './base-controller': {
      respondWith200: function(res, data) {
        data.displayName.should.equal('John Lennon');
      },
      respondWith400: this.respondWith400Spy,
      respondWith404: this.respondWith404Spy
    }
  });
  const req = {grft_session:{user:{email:'john@lennon.com'}}};
  const nextSpy = sinon.spy(function() {
    done();
  });
  controller.getMe(req, this.res, nextSpy);
  this.findOneSpy.should.have.been.called;
});
相关问题