2017-08-28 61 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

相關問題