简体   繁体   中英

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
          });
   }
 }
}

Test

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

Error: undefined is not a constructor evaluating this.retrieve({id: source.id}).

Can anyone help with what I might be doing wrong?

I have added a working example in stackblitz so you can play with it. If you want to mock a function you can use "and.callFake" in Jasmine.

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

Here is the link to stackblitz. https://stackblitz.com/edit/jasmine-testing-c2nicd?file=src%2Fhelpers.spec.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM