Joh*_*n Z 6 javascript unit-testing karma-jasmine angular
我正在尝试编写一个单元测试,以查看是否调用了基类方法
这是基类
export abstract class Animal{
protected eatFood() {
console.log("EAT FOOD!")l
}
}
Run Code Online (Sandbox Code Playgroud)
这是我要测试的课程
export class Monkey extends Animal {
onHungry(){
this.eatFood();
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试
class MockAnimal {
public eatFood() {
console.log("EAT MOCKED FOOD!");
}
}
describe('Monkey', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations:[Monkey],
providers: [
{ provide: Animal, useClass: MockAnimal }
]
}
});
it('eat food when hungry', fakeAsync(() => {
let fixture = TestBed.createComponent(Monkey);
spyOn(fixture, 'eatFood');
fixture.componentInstance.onHungry();
expect(fixture.eatFood).toHaveBeenCalled();
}));
}
Run Code Online (Sandbox Code Playgroud)
我无法运行该MockAnimal类的单元测试。这是测试的最佳方法吗?
抱歉,这是一个菜鸟问题,我只是从angular 2开始
任何帮助将不胜感激。
谢谢
小智 -1
你可以这样做,这会嘲笑你的电话。
it('eat food when hungry', fakeAsync(() => {
let fixture = TestBed.createComponent(Monkey);
fixture.componentInstance['eatFood'] = jasmine.createSpy('eatFood');
fixture.componentInstance.onHungry();
expect(fixture.eatFood).toHaveBeenCalled();
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
993 次 |
| 最近记录: |