Return return 值 Jasmine 的延迟对象

Return deferred object for return value Jasmine

我正在尝试为我正在测试的 Angular2 组件在 jasmine 中伪造一个 return 的延迟对象。

组件函数:

 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 不会 return 延迟之外,这非常有效。

fakeTask.execute() 的预期结果将 return 一个像这样的对象:

 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 的执行间谍设置为 return 延迟是否可行? (真正的东西 return 是一个 Deferred Dojo 类型,如上所示。 http://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html)

这解决了它:

 import Deferred from "dojo/Deferred";

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

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