【发布时间】:2022-01-18 14:26:53
【问题描述】:
在组件 .ts 文件中
onScrollToProductSelector(){
let offset = document.getElementById("selector").offsetTop;
window.scrollBy({
top: offset,
behavior: "smooth"
});
}
在 .spec.ts 文件中
it("should call window.scrollby", () => {
spyOn<any>(window, "scrollBy");
let topOffset = fixture.debugElement.nativeElement.querySelector("#selector").offsetTop;
const mockObj = {
top: topOffset,
behavior: "smooth"
}
component.onScrollToProductSelector();
expect(window.scrollBy).toHaveBeenCalledWith(mockObj);
});
但是当我运行这个测试用例时,我收到以下错误:
错误 TS2554:预期有 2 个参数,但得到了 1 个。
【问题讨论】:
-
在哪一行显示错误?
-
@AakashGarg 这一行
expect(window.scrollBy).toHaveBeenCalledWith(mockObj); -
尝试将 expect(window.scrollBy).toHaveBeenCalledWith(mockObj) 更改为 expect(window.scrollBy).toHaveBeenCalledWith(mockObj, undefined)。如果可行,请告诉我,我会将其作为答案。
-
@aakashgarg 没用。现在它给第一个参数另一个错误说: {top: number, behavior:string} 类型的参数不能分配给 number 类型的参数 | asymetricMatcher
标签: angular jasmine karma-jasmine angular11