angular - jasmine callFake 未被使用;真正的函数仍然被调用
问题描述
我正在尝试使用 spyOn 和 callFake 来模拟函数调用,但仍在调用真正的函数而不是假函数。
组件代码:
async getOrderProfile() {
let ordersSuccess = false;
...
try {
const ordersResponse = await this.ordersService.getOrderProfile(params);
ordersSuccess = this.validateResponse(ordersResponse);
...
} catch (error) {
...
}
}
规格:
describe('OrdersComponent', () => {
let component: OrdersComponent;
let fixture: ComponentFixture<OrdersComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [OrdersComponent],
imports: [],
providers: []
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(OrdersComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call getOrdersNoteData', () => {
...
spyOn(component, 'validateResponse').and.callFake(() => {
console.log('faked method');
return true;
});
component.getOrderProfile();
...
});
});
我没有看到我的测试语句记录到控制台,真正的函数抛出错误,因为它没有所有预期的数据。我不确定我在这方面做错了什么。
我也没有运气使用过 spyOn returnValue:
spyOn(component, 'validateResponse').and.returnValue(true)
解决方案
推荐阅读
- java - 如何使 Kotlin 库像 Java 库一样?
- python - 如何使用卡尔曼滤波器模型检测峰值
- android - 滚动时recyclerview中的两个复选框更改状态
- angularjs - 为什么使用 AngularJS 在 HTML 页面中显示 JSON 答案的 {{variable_name}} 不起作用?
- html - css中的过渡/动画后元素不需要的小移动
- docker - 在 docker 中构建 rust 项目会导致 Cargo 无法下载依赖项
- javascript - 如何从 href 属性获取宽度和高度
- php - PDO 没有将 MS Access DB 的输出发送到 php
- mongodb - MongoDB多组聚合
- php - AuthnetWebhook 包签名密钥验证