簡體   English   中英

Jasmine - undefined 不是構造函數

[英]Jasmine - undefined is not a constructor

export default {
  retrieve() {...},
  process: {
    getData(source) {
      return this.retrieve({id: source.id})
         .then((reply) => {
              source.reply = reply;
              return reply
          });
   }
 }
}

測試

describe("getData", () => {
    const source = { id: 1 };
    it("calls #retrieve", () => {
        spyOn(helpers, "retrieve").and.returnValue(PromiseSpy);
        helpers.process.getData(source);
        expect(helpers.retrieve).toHaveBeenCalled();
    });
});

錯誤:undefined 不是評估 this.retrieve({id: source.id}) 的構造函數。

任何人都可以幫助我解決我可能做錯的事情嗎?

我在 stackblitz 中添加了一個工作示例,因此您可以使用它。 如果你想模擬一個函數,你可以在 Jasmine 中使用“and.callFake”。

spyOn(helpers, "fetch").and.callFake(
      () => new Promise((resolve, reject) => resolve({data: "mocked"}))
    );
const data = await helpers.getList(1);
expect(data).toBe("mocked"); 

這是stackblitz的鏈接。 https://stackblitz.com/edit/jasmine-testing-c2nicd?file=src%2Fhelpers.spec.ts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM