单元测试逻辑内部承诺回调

yam*_*ade 2 javascript jasmine es6-promise aurelia

我有一个ES6/Aurelia应用程序,我正在使用茉莉花进行测试.我试图测试的方法看起来像这样:

update() {
    let vm = this;
    vm.getData()
        .then((response) => {
            vm.processData(response);
        });
}
Run Code Online (Sandbox Code Playgroud)

this.getData返回promise的函数在哪里.

我的spec文件看起来像这样:

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

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

        // this one fails
        expect(myService.processData).toHaveBeenCalled();
    });
});
Run Code Online (Sandbox Code Playgroud)

我理解为什么会失败 - 承诺是异步的,并且在它达到预期时尚未得到解决.

如何从我的测试中推出解决方案以便我可以测试回调中的代码?

失败测试的jsfiddle:http://jsfiddle.net/yammerade/2aap5u37/6/

yam*_*ade 6

我通过返回一个行为类似于promise而不是实际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();
    });
});
Run Code Online (Sandbox Code Playgroud)

在这里小提琴:http://jsfiddle.net/yammerade/9rLrzszm/2/

这样做有什么不对吗?