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?
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.
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").
SpyOn is a Jasmine feature that allows dynamically intercepting the calls to a function and change its result.
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.
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.
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: ?
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 ?
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With