0
在我的代码中,我有一个方法返回一个零件对象,但它返回setTimeout
方法中的对象。当我试图测试这个时,我遇到了这样的问题,即测试不会等待超时完成,然后才会评估该方法的响应。如何在测试期望之前告诉测试等待超时完成?Angular 2 Jasmine在继续之前等待超时
正如你所看到的getIntersection
方法在超时时间内返回this.part。
public getIntersection(payload: DropPayload): Part {
let rect: ClientRect = this.element.nativeElement.getBoundingClientRect();
if (rect.left < payload.event.pageX && rect.right > payload.event.pageX && rect.top < payload.event.pageY && rect.bottom > payload.event.pageY) {
setTimeout(() => {
this.changeRef.detectChanges();
return this.part;
});
}
return null;
}
这里的一些事情我已经基于研究这个问题的尝试,他们没有工作:
1:
it('return the part if the payload coordinates are inside the bounding box', async((done) => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
done();
});
}));
2:
it('return the part if the payload coordinates are inside the bounding box', async(() => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
});
}));
3: (这个抛出的错误是runs
没有定义,我相信这是一个Angular 1的解决方案)
it('return the part if the payload coordinates are inside the bounding box',() => {
let done: boolean = false;
let result: any;
runs(() => {
partInstance.part = Clone.create(mockPart);
result = partInstance.getIntersection(mockPayload);
setTimeout(() => {
done = true;
});
});
waitsFor(() => {
return done;
});
runs(() => {
expect(result).toEqual(mockPart);
});
});
4:
it('return the part if the payload coordinates are inside the bounding box',() => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
});
});