Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jasmine spy on nested object

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?

like image 207
Amitava Avatar asked Jun 15 '13 06:06

Amitava


People also ask

How do you spy on component property 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 a Jasmine spy object?

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.

What is callFake Jasmine?

callFake(fn)Tell the spy to call a fake implementation when invoked.

What is toHaveBeenCalled in Jasmine?

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.


1 Answers

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.

like image 183
Amitava Avatar answered Sep 29 '22 23:09

Amitava