【问题标题】:Angular async unit test finishes prematurely角度异步单元测试过早完成
【发布时间】:2018-06-13 18:28:13
【问题描述】:

以下针对 Angular 的单元测试(使用 Jasmine-Karma)提前完成,而没有到达测试的结尾。请帮忙。

输出:

LOG: '{"a":"b"}'
LOG: '1'
LOG: 'Finished!'

错误:

null: Unhandled Promise rejection: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out ; Zone: ProxyZone ; Task: Promise.then ; Value:
null: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out

单元测试:

import 'rxjs/add/observable/of';

import { } from 'jasmine';
import { TestBed, inject } from '@angular/core/testing';
import { async } from '@angular/core/testing';
import { Observable } from 'rxjs/Observable';

interface FileResponse {
    data: Blob;
}

class BlobUtil {
    public static asString(blob: Blob) {
        return new Promise<string>((resolve, reject) => {
            var reader = new FileReader();
            reader.onload = (event: any) => resolve(event.target.result);
            try {
                reader.readAsText(blob);
            }
            catch (e) {
                reject(e);
            }
        });
    }
}

class Service {
    public static async SafeCall(executor: Observable<FileResponse>,
        success: (response: string) => void, failure: (error: string) => void) {
        try {
            var response = await executor.toPromise();
            success(await this.Handle(response));
        }
        catch (e) {
            failure(e.message);
        }
    }

    private static async Handle(response: FileResponse) {
        return await BlobUtil.asString(response.data);
    }
}

describe('Tests', () => {

    afterEach(() => {
        console.log("Finished!");
    });

    it("test", async(inject([],
        async () => {
            var request = Observable.of({ data: new Blob(['{"a":"b"}'], { type: 'text/plain' }) });
            await Service.SafeCall(request, response => console.log(response), error => console.log(error));
            expect(true).toBeTruthy();
            console.log("1");
            await Service.SafeCall(request, response => console.log(response), error => console.log(error));
            console.log("2");
            expect(true).toBeTruthy();
            console.log("3");
            await Service.SafeCall(request, response => console.log(response), error => console.log(error));
            console.log("4");
            expect(true).toBeTruthy();
            console.log("5");
        })));
    });

【问题讨论】:

    标签: angular unit-testing typescript jasmine karma-jasmine


    【解决方案1】:

    这看起来像是 Angular 的默认单元测试框架 Jasmine 的一个基本问题(是时候看看你的单元测试覆盖率了——这太基本了,不会在发布中失败)。我找到了一种解决方法,直到他们能够正确修复它。

    将 async 函数替换为以下内容,它会按预期工作。

    export function async(specFunc) {
      return (done) => {
          specFunc().then(() => {
              done();
          }).catch((error) => {
              done.fail(error);
          });
      };
    }
    
    // it("test", async(inject([], ...
    

    附言我在 Jasmine 的 GitHub 存储库上找不到任何单元测试...???? 请告诉我有单元测试!

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 2021-07-08
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多