我想对下面的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构造,以模拟getEvents
的internalService
测试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)