使用Sinon将存根依赖项注入到es6类中

时间:2017-09-25 18:08:58

标签: javascript mocha sinon stub es6-class

我有以下模块:

const rp = require('request-promise');

// dummies.repository.js 

exports.getDummiesData = () => {
  const options = {
    url: 'https://api.github.com/users',
    headers: {
      'User-Agent': 'Request-Promise',
    },
    json: true
  };
  return rp(options)
  .then(r => r)
  .catch(err => { throw err; });

}

上面的文件是我想要测试的类的依赖项。我通过它的构造函数将它注入下面的类:

// dummies.service.js
const Dummies = require('./dummies.model');
class DummiesService {
  constructor(dummiesRepository) {
  this.dummiesRepository = dummiesRepository;
 }

 getDummies() {
    return new Promise((resolve, reject) => {
        this.dummiesRepository.getDummiesData()
        .then((response) => {
        // business logic
        const dummies = response.map(d => new Dummies(d));
        resolve(dummies);
       })
       .catch((response) => {
         reject(response);
       });
   });
  }
 }
module.exports = DummiesService;

在上面,我想用sinon测试方法getDummies。到目前为止,我的方法是将dummiesRepository存根并在实例化期间将其注入我的傻瓜服务,但是我收到了错误。以下是我的测试结果:

const DummiesService = require('../dummies.service');
const dummiesRepository = require('../dummies.repository');
let dummiesRepositoryStub;

afterEach(() => {
   if(dummiesRepositoryStub){
     dummiesRepositoryStub.restore();
   }
});

describe('unit: dummies.service - when getting dummies data', () => {
  it('it should resolve and return dummies data', () => {
    const dummiesResponse = [
      { id: 1, login: 'dummy1', additionalData: 'data' },
      { id: 2, login: 'dummy2', additionalData: 'data' },
      { id: 3, login: 'dummy3', additionalData: 'data' },
    ];

    dummiesRepositoryStub = sinon
      .stub(dummiesRepository, 'getDummiesData')
      .resolves(dummiesResponse);

    dummiesService = new DummiesService(dummiesRepositoryStub);
    // act + assert
    return dummiesService.getDummies()
      .then((response) => {
        chai.expect(dummiesRepositoryStub.called).to.equal(true);
        chai.expect(response).to.deep.equal(dummiesTransformedModel);
      });
  });

此测试失败,错误为TypeError: this.dummiesRepository.getDummiesData is not a function。我不确定为什么这不起作用,我将dummiesRepository的存根实现注入DummiesService类。请协助。

1 个答案:

答案 0 :(得分:0)

我找不到在上面的代码中进行构造函数注入的方法,但我找到了一个解决方法,允许sinon存根我的方法。

我不得不将dummyRepository从构造函数依赖项中移除,只使用require来包含它。如果代码如下所示

const Dummies = require('./dummies.model');
const dummiesRepository = require('./dummies.repository');

class DummiesService {
  constructor() {
    this.dummiesRepository = dummiesRepository;
  }
  getDummies() {
    return new Promise((resolve, reject) => {
      this.dummiesRepository.getDummiesData()
        .then((response) => {
          // business logic
          const dummies = response.map(d => new Dummies(d));
          resolve(dummies);
        })
        .catch((response) => {
          reject(response);
        });
    });
  }
}

module.exports = DummiesService;

并且测试的编写与上面相同,但是在没有DummyService作为参数的情况下实例化dummyRepositoryStub,一切正常。

这有效,但我仍然想知道是否有办法通过构造函数注入依赖项,因为这种方法需要我重构我的很多代码。