我尝试在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不返回deferred之外,这一点非常有效。
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的execute spy返回deferred是否可行(真实的的返回一个Deferred Dojo类型,如上所示。http://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html)
1条答案
按热度按时间jv4diomz1#
这解决了它:
这使得对象类型可识别为延迟类型。