2017-08-28 65 views
0

我一直在寻找正确的方法来测试使用Jasmine的Angular 4单元测试中的GraphQL异步调用。 什么是不使用setTimeout函数测试异步调用的正确流程? 这是我当前的代码而不是等待响应在angular4单元测试中处理异步调用的正确方法是什么?

it('shoud swap', (done) => { 
const SequenceMaster = '0'; 
const sequenceMasterAlts = '2138'; 
const sKey = '[DARCARS]SS_02_VW+201701_VW'; 
component.swapTablesData(sKey); 
    expect(component.testVariable).toBe(true); 
    done(); 
}); 

回答

0

Docs虽然被解释相当不错的,你可以使用fakeAsync做到这一点:

describe('test service using fakeAsync', fakeAsync(() => { 
    let service: SomeService; 

    beforeEach(() => { 
     TestBed.configureTestingModule({ providers: [SomeService] }); 

     service = TestBed.get(SomeService, null); 
    }); 

    it('should use SomeService',() => { 
     let returnValue: any; 
     service.doSomething().then((val: any) => returnValue = val); 
     tick(); 
     expect(returnValue).toBe('return value using fakeAsync'); 
    }); 
}); 
+0

我已经按照这些指令。但是在异步调用之前所有执行都是 –

+0

你是什么意思? – fsi

相关问题