Home > Software engineering >  Is there a way to mock functions from a jasmine Spy
Is there a way to mock functions from a jasmine Spy

Time:12-15

I'm using jasmine to create a spy object, and returning an object, can I mock functions from the object I'm returning?

For example:

let mockService = jasmine.createSpyObj(['fun']);
mockService.fun.and.returnValue({value: 1});

I'm trying to mock the get function in this example:

let x = service.fun();
x.get();

CodePudding user response:

Just use jasmine.createSpyObj() method to create spy obj for the return value of service.fun().

describe('70304592', () => {
  it('should pass', () => {
    const funSpy = jasmine.createSpyObj(['get']);
    funSpy.get.and.returnValue('1');
    let serviceSpy = jasmine.createSpyObj(['fun']);
    serviceSpy.fun.and.returnValue(funSpy);
    const x = serviceSpy.fun();
    expect(x.get()).toBe('1');
  });
});
  • Related