2

Can anyone shed some light on this error I'm seeing in my Jasmine unit tests where I'm creating a spy on a mock service call and trying to return a value?

This is an example of the spy:

mockService = jasmine.createSpyObj('MyService', ['getSomeId']);
mockService.getSomeId.and.returnValue(10);

When i run ng test, the following error shows up in the console window - but the test actually passes successfully in the browser:

error TS2339: Property 'and' does not exist on type '() => string'. src/app/services/myservice.service.spec.ts(57,45): error TS2339: Property 'and' does not exist on type '(controller: string) => string'.

Any ideas?

  • Is `(controller: string) => string` the signature of the actual implementation? Do you see the same error when using `(mockService.getSomeId as jasmine.Spy).and.returnValue(10);`? – Alex Szabo Sep 17 '18 at 19:05
  • 2
    You're right, casting it that way worked fine - with that, I realized i was setting the type of my mock (spy) service incorrectly. – Modern Viking Sep 17 '18 at 21:25

1 Answers1

7

Alex shed some light on the issue - I was strongly typing the mock service as the service type, instead of a spy type. Once I removed the type, the error went away.