简体   繁体   English

在angular4单元测试中处理异步调用的正确方法

[英]Correct way to handle async calls in angular4 unit test

I have been searching for correct method to test GraphQL asynchronous calls in Angular 4 unit test using Jasmine. 我一直在寻找使用Jasmine在Angular 4单元测试中测试GraphQL异步调用的正确方法。 What is the correct flow of testing asynchronous calls without using setTimeout function ? 不使用setTimeout函数测试异步调用的正确流程是什么? This is my current code but not waiting for response 这是我当前的代码,但不等待响应

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();
 });

Docs are explaining pretty nice though and you can do this using fakeAsync : 不过, 文档说明的很好,您可以使用fakeAsync做到这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');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM