我正在尝试假装在jasmine中返回一个延迟对象,对于我正在测试的Angular2组件。
组件功能:
public createAndExecuteIdentify(url) {
identifyTask = this._widgetFactory.createIdentifyTask(url); //returns object
return identifyTask.execute(this.identifyParams); //this returns a deferred
}
规格:
let fakeTask: any;
fakeTask = {
execute: jasmine.createSpy('execute'),
url: "bob@bob.com"
};
fakeFactory.createIdentifyTask.and.returnValue(fakeIdentifyTask);
describe("when executing identifyTask", () => {
it("should return the proper object", () => {
let taskReturn = component.createAndExecuteIdentify(fakeTask.url);
expect(taskReturn).toEqual(jasmine.objectContaining({
promise: {}
}));
});
});
除了fakeTask.execute不返回延迟这一事实外,这个工作很好。
fakeTask.execute()的所需结果将返回如下对象:
b.Deferred {promise: {…}, isResolved: ƒ, isRejected: ƒ, isFulfilled: ƒ,
isCanceled: ƒ, …}
addCallbacks: ƒ (a,b)
callback: ƒ (a)
cancel: ƒ ()
errback: ƒ (a)
isCanceled: ƒ ()
isFulfilled: ƒ ()
isRejected: ƒ ()
isResolved: ƒ ()
progress: ƒ (a)
promise: {isResolved: ƒ, isRejected: ƒ, isFulfilled: ƒ, isCanceled: ƒ, then: ƒ, …}
reject: ƒ (a)
resolve:ƒ (a) then: ƒ (b,d,h)
_pendingDfd: b.Deferred {promise: {…}, isResolved: ƒ, isRejected: ƒ, isFulfilled: ƒ, isCanceled: ƒ, …}
__proto__: Object
所以我想知道是否以某种方式设置fakeTask的执行间谍来返回延迟会有效吗? (真实的东西返回Deferred Dojo类型,如上所示。 http://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html)
答案 0 :(得分:0)
这解决了它:
import Deferred from "dojo/Deferred";
let deferred = new Deferred();
fakeTask.execute.and.returnValue(deferred);
这使得对象类型可识别为延迟类型。