相关疑难解决方法(0)

使用Sinon.JS模拟JavaScript构造函数

我想对下面的ES6类进行单元测试:

// service.js
const InternalService = require('internal-service');

class Service {
  constructor(args) {
    this.internalService = new InternalService(args);
  }

  getData(args) {   
    let events = this.internalService.getEvents(args);
    let data = getDataFromEvents(events);
    return data;
  }
}

function getDataFromEvents(events) {...}

module.exports = Service;
Run Code Online (Sandbox Code Playgroud)

我如何嘲笑与Sinon.JS构造,以模拟getEventsinternalService测试getData

我查看了使用Sinon的Javascript:Mocking Constructor,但无法提取解决方案.

// test.js
const chai = require('chai');
const sinon = require('sinon');
const should = chai.should();

let Service = require('service');

describe('Service', function() {
  it('getData', function() {
    // throws: TypeError: Attempted to wrap undefined property …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocking node.js sinon

25
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×1

mocking ×1

node.js ×1

sinon ×1

unit-testing ×1