Jasmine测试一个回调传递给anonym函数

时间:2018-02-02 16:35:48

标签: javascript node.js karma-jasmine aurelia

Hellow,

我有类似

的东西
export class Api {
    callHttpClient(url, options, settings) {
        this.httpClient.configure(callbackObjectInstance => {
            callbackObjectInstance.method();
        }); // And then some code
    }
}

我怎么能用Jasmine测试框架监视callbackObject.method?

谢谢

1 个答案:

答案 0 :(得分:0)

这应该是一种方法。

describe('Given the API', function(){
    let api;
    let callback;
    beforeEach(function(){
        api = new Api();
        callback = {
            method: function() { }
        };
        spyOn(callback, 'method');
        spyOn(api.httpClient, 'configure').and.returnValue(callback);
    });

    it('should call method', function(){
        api.callHttpClient(something, something, something);
        expect(callback.method).toHaveBeenCalledTimes(1);
    });
});