【问题标题】:Sinon Class ConstructorSinon 类构造函数
【发布时间】:2020-08-26 05:20:57
【问题描述】:

我有一个 Animal 类如下

Animal.js

export default class Animal {
  constructor(type) {
    this.type = type
  }
  getAnimalSound(animal) {
    if (animal && animal.type == 'dog') return 'woof'
    if (animal && animal.type == 'cat') return 'meow'
  }
}

我制作了一个动物园模块,它有一个 getAnimalSound() 方法,如下所示

动物园.js

import Animal from './Animal'

export default function getAnimalSound(type) {
  let animal = new Animal(type)
  let animalSound = animal.getAnimalSound(animal)
  return animalSound
}

现在我如何对 zoo 模块进行单元测试?

zoo.test.js

import sinon from 'sinon'

import Animal from './Animal'
import getAnimalSound from './zoo'

let animalStub = sinon.createStubInstance(Animal)
let a = animalStub.getAnimalSound.returns('woof')
let sound = getAnimalSound('cat')
console.log(sound)

所以问题是“新”对我在 test.js 中存根的方式没有影响 我能做到吗?

问候 波布P

【问题讨论】:

    标签: node.js unit-testing sinon stub


    【解决方案1】:

    您可以使用Link Seams 模拟您的./animal.js 模块和Animal 类。

    例如

    animal.ts:

    export default class Animal {
      type: any;
      constructor(type) {
        this.type = type;
      }
      getAnimalSound(animal) {
        if (animal && animal.type == 'dog') return 'woof';
        if (animal && animal.type == 'cat') return 'meow';
      }
    }
    

    zoo.ts:

    import Animal from './animal';
    
    export default function getAnimalSound(type) {
      let animal = new Animal(type);
      let animalSound = animal.getAnimalSound(animal);
      return animalSound;
    }
    

    zoo.test.ts:

    import sinon from 'sinon';
    import proxyquire from 'proxyquire';
    import { expect } from 'chai';
    
    describe('61716637', () => {
      it('should pass', () => {
        const animalInstanceStub = {
          getAnimalSound: sinon.stub().returns('stubbed value'),
        };
        const AnimalStub = sinon.stub().returns(animalInstanceStub);
        const getAnimalSound = proxyquire('./zoo', {
          './animal': { default: AnimalStub },
        }).default;
        const actual = getAnimalSound('bird');
        expect(actual).to.be.equal('stubbed value');
        sinon.assert.calledWith(AnimalStub, 'bird');
        sinon.assert.calledWith(animalInstanceStub.getAnimalSound, animalInstanceStub);
      });
    });
    

    带有覆盖率报告的单元测试结果:

      61716637
        ✓ should pass (2242ms)
    
    
      1 passing (2s)
    
    -----------|---------|----------|---------|---------|-------------------
    File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    -----------|---------|----------|---------|---------|-------------------
    All files  |   54.55 |        0 |   33.33 |   66.67 |                   
     animal.ts |   16.67 |        0 |       0 |      25 | 4-8               
     zoo.ts    |     100 |      100 |     100 |     100 |                   
    -----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2016-05-22
      • 2015-06-04
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多