Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript + jasmine.createSpy()

I'm writing unit tests for my angular project with Typescript

When I try to create mock for some service, I use this way:

const serviceMock = <IMyService>{
    method: _.noop,
};

beforeEach(inject($injector => {
   testingService = new AccountingService(serviceMock);

   spyOn(serviceMock, 'method').and.callFake(()=>'hello');
 }

this works ok but when I try to use jasmine.createSpy(), I get compilation errors:

const serviceMock = <IMyService>{
    method: jasmine.createSpy('method').and.callFake(()=>'hello'),
};

Type '{ method: Spy;}' cannot be converted to type 'MyService'. Property 'getParams' is missing in type '{ method: Spy;}'.

But getParams is private method of MyService

What am I doing wrong?

like image 971
Katya Pavlenko Avatar asked Jun 21 '17 08:06

Katya Pavlenko


People also ask

How do you use Jasmine createSpy?

Jasmine: createSpy() and createSpyObj() Jasmine's createSpy() method is useful when you do not have any function to spy upon or when the call to the original function would inflict a lag in time (especially if it involves HTTP requests) or has other dependencies which may not be available in the current context.

How do you make a spy house in Jasmine?

In Jasmine, you can do anything with a property spy that you can do with a function spy, but you may need to use different syntax. Use spyOnProperty to create either a getter or setter spy. it("allows you to create spies for either type", function() { spyOnProperty(someObject, "myValue", "get").

What is the use of spyOn in Jasmine?

SpyOn is a Jasmine feature that allows dynamically intercepting the calls to a function and change its result.

How do you spy on Jasmine classes?

If you want to detect when that method gets called on any instance of ClassName anywhere, you need to spy on the prototype. beforeEach(function() { spyOn(ClassName. prototype, 'doA'); }); it('should call doA', function() { myFunc(); expect(ClassName. prototype.

What is the use of createspy () method in Jasmine?

Jasmine: createSpy() and createSpyObj() Jasmine's createSpy()method is useful when you do not have any function to spy upon or when the call to the original function would inflict a lag in time (especially if it involves HTTP requests) or has other dependencies which may not be available in the current context.

What is a spy object in Jasmine?

Jasmine spies are a great and easy way to create mock objects for testing. By using a Spy object, you remove the need to create your own function and class stubs just to satisfy test dependencies. Let’s say you have this service for saving a person: ?

What is the difference between createspyobj and spyobj in typescript?

In TypeScript, the compiler needs a bit of help. createSpyObj returns either any or SpyObj<T>, and a SpyObj only defines the methods as being spied on: type SpyObj<T> = T & { [K in keyof T]: T [K] extends Func ?

What is the use of createspy () method in JSON?

Jasmine 's createSpy () method is useful when you do not have any function to spy upon or when the call to the original function would inflict a lag in time (especially if it involves HTTP requests) or has other dependencies which may not be available in the current context.


1 Answers

Use the type already defined and used by Jasmine SpyObj<T>.

const serviceMock: jasmine.SpyObj<IMyService> = jasmine.createSpyObj<IMyService>('service',['method']);

This way every method of IMyService will be augmented with the Spy ones:

serviceMock.method.and.callFake(()=>'hello');
like image 77
novembre Avatar answered Oct 17 '22 06:10

novembre