我在angular的组件中有下一个方法:
private onTopClick() {
$('body,html').animate({ scrollTop: 0 });
}
我如何用茉莉花来测试?只是为了检查是否已经调用了“animate”方法。
答案 0 :(得分:1)
你需要使用几个函数......
首先,为animate
功能设置间谍。您可能希望在所有测试中或在每次测试之前执行此操作,也可能仅用于一次测试;您可能还希望在间谍拦截通话后执行此操作,例如.and.callFake()
或.and.callThrough()
等等。
beforeEach(function() {
spyOn($.fn, "animate");
});
在实际测试内部检查是否调用了animate
函数。可能看起来像......
it("should call '$(selector).animate'", function () {
onTopClick();
expect($.fn.animate).toHaveBeenCalled();
});
有关Jasmine Testing的更多信息over here。