Unit testing logic inside promise callback

£可爱£侵袭症+ 提交于 2019-12-06 13:09:13

I got a workaround running by returning an object that behaves like a promise instead of an actual promise

describe('my service update function', () => {
    it('it will call the other functions', () => { 
        myService = new MyService();
        spyOn(myService, 'getData').and.returnValue({
            then(callback) {
                callback();
            }
        });
        spyOn(myService, 'processData').and.callFake(function() { return; });
        myService.update();

        // this one passes
        expect(myService.getData).toHaveBeenCalled();

        // this one fails
        expect(myService.processData).toHaveBeenCalled();
    });
});

Fiddle here: http://jsfiddle.net/yammerade/9rLrzszm/2/

Is there anything wrong with doing it this way?

it((done) => {
  // call done, when you are done
  spyOn(myService, 'processData').and.callFake(function() {
    expect(myService.processData).toHaveBeenCalled();

    done();
  });
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!