My service object looks like this:
var appService = {
serviceOne: {
get: function(){}
},
serviceTwo: {
query: function(){}
}
}
I would like to mock appService,something like:
expect(appService.serviceTwo.query).toHaveBeenCalled();
How would I go about doing it?
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").
A Spy is a feature of Jasmine that allows you to stub any function and track calls to it back. It's usually used to mock a function or an object.
callFake(fn)Tell the spy to call a fake implementation when invoked.
The toHaveBeenCalled() matcher verifies whether the spied method has been called or not. It returns true if the spy was called. The following spec returns true as the method circumference() is called.
OK I got this working with this:
appService: {
serviceOne: jasmine.createSpyObj('serviceOne', ['get']),
serviceTwo: jasmine.createSpyObj('serviceTwo', ['query'])
}
I hope it is the right way to do.
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