[英]How to call a fake function in Angular 9 component unit test
I have an angular component containing two methods我有一个包含两种方法的角度组件
class MyComponent {
constructor() {
...
}
loadDataSource() {
...
}
runFilter() {
//some other logic here
this.loadDataSource();
}
}
And I have a unit test我有一个单元测试
describe('running filters', () => {
beforeEach(() => {
component.runFilter();
const spy = jasmine.createSpyObj(['loadDataSource']);
spy.loadDataSource.and.callFake(function() { });
});
it('should call load data source', () => {
expect(component.loadDataSource).toHaveBeenCalled();
});
});
However, this seems to still call the actual implementation of loadDataSource when runFilter is called, instead of the empty fake function I provided.但是,这似乎仍然在调用 runFilter 时调用了 loadDataSource 的实际实现,而不是我提供的空假函数。
How may I test that loadDataSource method was called when I call runFilter, without actually invoking the actual implementation?我如何测试调用 runFilter 时调用的 loadDataSource 方法,而不实际调用实际实现? The reason for doing so is I do not want any of the logic for loadDataSource invoked.
这样做的原因是我不希望调用 loadDataSource 的任何逻辑。
Try this尝试这个
describe('running filters', () => {
beforeEach(() => {
spyOn(component, 'loadDataSource').and.callFake();
});
it('should call load data source', () => {
component.runFilter();
expect(component.loadDataSource).toHaveBeenCalled();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.