Sinon Class 构造函数
Sinon Class Constructor
我有一个动物class如下
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() 方法,如下所示
zoo.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)
所以问题是 'new' 对我在 test.js 中存根的方式没有影响
我能做到吗?
问候
波布P
您可以使用 Link Seams 模拟您的 ./animal.js
模块和 Animal
class.
例如
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 |
-----------|---------|----------|---------|---------|-------------------
我有一个动物class如下
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() 方法,如下所示
zoo.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)
所以问题是 'new' 对我在 test.js 中存根的方式没有影响 我能做到吗?
问候 波布P
您可以使用 Link Seams 模拟您的 ./animal.js
模块和 Animal
class.
例如
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 |
-----------|---------|----------|---------|---------|-------------------