在Jasmine中有函数.toHaveBeenCalledWith()和.toBeTruthy(),但是有没有一种方法来检查一个函数是否被调用了真值呢?
据我所知,对于.toHaveBeenCalledWith(),您必须传递一个确切值,但我只想检查是否使用真值(例如,不是未定义的或空的)调用了函数。
发布于 2019-01-10 14:38:01
你可以做的是在你试图测试参数的函数上设置一个间谍。然后通过间谍获得论点,例如let my arg = mySpy.calls.mostRecent().args[0];和do a simple e.g. expect(arg).toBeTruthy()。
伪代码片段:
let mySpy = spyOn(myComponent, 'myFunctionToSpyOn').and.callThrough();
// do something that triggers the function call
let arg = mySpy.calls.mostRecent().args[0];
expect(arg).toBeTruthy();https://stackoverflow.com/questions/54113172
复制相似问题