[英]Unit testing ionic function which returns or uses a promise - TS2304: Cannot find name 'done' - Jasmine / Karma for unit testing ionic apps
I'm trying to unit test a simple data access function with mocked Storage that simply uses a dictionary in memory. 我正在尝试对模拟的存储(仅在内存中使用字典)进行简单数据访问功能的单元测试。
When I try to use the done()
function I get the error: TS2304: Cannot find name 'done'
despite jasmine and karma seemingly being installed correctly. 当我尝试使用
done()
函数时,出现错误: TS2304: Cannot find name 'done'
尽管看似已正确安装了茉莉花和业力,却TS2304: Cannot find name 'done'
。
Things I've done that have not fixed this issue: 我所做的事情并未解决此问题:
data.ts: 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: 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: 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();
});
}
Actual answer: 实际答案:
done
is not a function that is part of the Jasmine framework - rather it is used within a test's declaration as a callback for when the test finishes. done
不是Jasmine框架的一部分,而是在测试的声明中用作测试完成时的回调。 Notice in the below the (done)
declaration: 注意以下
(done)
声明:
it("should test something async", (done) => {
...do the testing...
done();
}
Original answer: 原始答案:
While I am not sure why I can't use done()
, it seems that using fakeAsync, flushMicrotasks, tick
kind of sort of gets the job "done" (I am still having some async issues with the storage mock). 虽然我不确定为什么不能使用
done()
,但似乎使用fakeAsync, flushMicrotasks, tick
的方式会使工作“完成”(存储模拟仍然存在一些异步问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.