编程语言
首页 > 编程语言> > javascript-Sinonjs如何存根依赖注入类?

javascript-Sinonjs如何存根依赖注入类?

作者:互联网

我在下面编写了示例代码(从我的实际代码实现中进行了简化),此简化版本的主要思想是理解如何对类进行存根以进行单元测试.

我有将依赖项注入到Context类中的Sensor类,此Sensor将从某个IO端口获取数据.

传感器数据将在ComputeSensor类中进行比较以获取期望值,并将报告键插入context.report对象.

我是否可以知道如何对Sensor类进行存根或模拟,以便创建假值来测试代码?

class Sensor {
  getData() {        
    return {
      heat: this.getHeatSensor(), // get data from some IO
      speed: this.getSpeedSensor() // get data from some IO
    }
  }
}

class Context {
  constructor(sensor) {
    this.report = {};
    this.sensor = sensor;
    this.computeSensor = new ComputeSensor();
  }

  execute() {
    this.computeSensor.compute(this, this.sensor.getData());
  }
}

class ComputeSensor {
  compute(context, sensorData) {
    if (sensorData.heat === 123
      && sensorData.speed === 321) 
    {
      context.report = {
        sensor: 'ok'
      }  
    }
  }
}

const sensor = new Sensor();
const context = new Context(sensor);
context.execute();
console.log(context.report) // { sensor: 'ok }

也许所需的存根代码将是这样?

const stubSensor = sinon.createStubInstance(Sensor);
// Inject the stub return value here?
stubSensor.getData() = {
  heat: 123,
  speed: 321,
}

或者,我可以编写一个如下的模拟类..但是,我认为Sinon可以做到.

class MockSensor {
      getData() {
             return {
                   heat: 123,
                   speed: 321
             }
      }
 }

解决方法:

我希望我能正确理解.
您可以对方法进行存根,因此当有IO调用时,将返回固定值.
例如

import {expect} from 'chai';
import sinon from 'sinon';

class Sensor {
    getHeatSensor(){

    }
    getSpeedSensor(){

    }
    getData() {
        return {
            heat: this.getHeatSensor(), // get data from some IO
            speed: this.getSpeedSensor() // get data from some IO
        }
    }
}

class Context {
    constructor(sensor) {
        this.report = {};
        this.sensor = sensor;
        this.computeSensor = new ComputeSensor();
    }

    execute() {
        this.computeSensor.compute(this, this.sensor.getData());
    }
}

class ComputeSensor {
    compute(context, sensorData) {
        if (sensorData.heat === 123 && sensorData.speed === 321) {
            context.report = {
                sensor: 'ok'
            }
        }
    }
}

describe('Test Sensor', () => {
    it('should compute value ', () => {
        const sensor = new Sensor;
        sinon.stub(sensor,'getHeatSensor').returns(123);
        sinon.stub(sensor,'getSpeedSensor').returns(321);

        const context = new Context(sensor);
        context.execute();

        console.log(context.report);

        expect(context.report).to.deep.equal({sensor:'ok'})
        sensor.getHeatSensor.restore(); //don't forget to restore
        sensor.getSpeedSensor.restore();
    });

    it('should return empty object ', () => {
        const sensor = new Sensor;
        sinon.stub(sensor,'getHeatSensor').returns(99);
        sinon.stub(sensor,'getSpeedSensor').returns(84);

        const context = new Context(sensor);
        context.execute();

        console.log(context.report);

        expect(context.report).to.deep.equal({})
        sensor.getHeatSensor.restore();
        sensor.getSpeedSensor.restore();
    });

});

希望对此有所帮助,以进行澄清.

标签:stub,unit-testing,mocha,sinon,javascript
来源: https://codeday.me/bug/20191111/2021702.html