dojo 返回值Jasmine的返回延迟对象

e1xvtsh3  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(168)

我尝试在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

jv4diomz

jv4diomz1#

这解决了它:

import Deferred from "dojo/Deferred";

 let deferred = new Deferred();
 fakeTask.execute.and.returnValue(deferred);

这使得对象类型可识别为延迟类型。

相关问题