繁体   English   中英

返回或使用承诺的单元测试离子功能-TS2304:找不到名称“完成”-Jasmine / Karma用于离子测试离子应用

[英]Unit testing ionic function which returns or uses a promise - TS2304: Cannot find name 'done' - Jasmine / Karma for unit testing ionic apps

我正在尝试对模拟的存储(仅在内存中使用字典)进行简单数据访问功能的单元测试。

当我尝试使用done()函数时,出现错误: TS2304: Cannot find name 'done'尽管看似已正确安装了茉莉花和业力,却TS2304: Cannot find name 'done'

我所做的事情并未解决此问题:

  1. 经过验证的茉莉花在packages.json中
  2. 添加** / *。spec.ts以排除tsconfig.json的部分?
  3. 在tsconfig.json中将目标从“ e5”更改为“ e6”

data.ts:

export class DataProvider {
  private foo;
  public readonly fooKey
  public getFoo() { return this.foo; }
  public setFoo(bar: number) {
        this.foo = bar;
        this.storage.ready().then(() => {
            this.storage.set(this.fooKey, JSON.stringify(this.foo));
        });
    }
}

data.spec.ts:

include StorageMock;
include DataProvider;

 it('should have correct values after loading data',
       function() {
           comp.storage.set(comp.fooKey, JSON.stringify(0.1234));
           comp.storage.get(comp.fooKey).then(result => {
               expect(JSON.parse(result)).toEqual(0.1234);
               done(); // Error - TS2304: Cannot find name 'done'
           });
});

StorageMock:

export class StorageMock {
    private internal = [];

    public driver(): any {
        return '';
    }

    public ready(): Promise<any> {
        return new Promise(function(resolve: Function): void {
            resolve({});
        });
    }

    public get(key: string): Promise<any> {
        let getval = this.internal[key];
        return new Promise(function(resolve: Function): void {
            resolve(getval);
        });
    }

    public set(key: string, value: any): Promise<any> {
        this.internal.push({key : value});
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public remove(key: string): Promise<any> {
        let index = this.internal.indexOf(key);
        if(index !== -1) {
            this.internal.splice(index,1);
        }
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public clear(): Promise<any> {
        this.internal = [];
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public length(): Promise<any> {
        let length = this.internal.length;
        return new Promise(function(resolve: Function): void {
            resolve(length);
        });
    }

    public keys(): Promise<any> {
        let keys = Object.keys(this.internal);
        return new Promise(function(resolve: Function): void {
            resolve(keys);
        });
    }

    public forEach(i: any): Promise<any> {
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

实际答案:

done不是Jasmine框架的一部分,而是在测试的声明中用作测试完成时的回调。 注意以下(done)声明:

it("should test something async", (done) => {
 ...do the testing...
 done();
}

原始答案:

虽然我不确定为什么不能使用done() ,但似乎使用fakeAsync, flushMicrotasks, tick的方式会使工作“完成”(存储模拟仍然存在一些异步问题)。

暂无
暂无

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

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