lundi 27 juin 2016

What is difference between Jasmine CreateSpyObj and spyOn and advantage of one over another

I have been writing unit tests using Jasmine for an Angular App. during this I found out there are two API methods on Jasmine API viz

spyOn and createSpyObj

Would like to know what is the exact difference between the two and when I should use or have to use createSpyObj instead of spyOn? to make clear following is my code

'use strict';
describe('myService', function () {
var dependentService,dependentService1,rootScope,$q;
beforeEach(module('myModule.myConfig'));
beforeEach(module('myModule'));
beforeEach(inject(function (_myService_, _$rootScope_,
                   _$q_,_dependentService1_,_dependentService_) {
myService= _myService_;
rootScope = _$rootScope_.$new();
$q = _$q_;
dependentService1= _dependentService1_;
dependentService= _dependentService_;

spyOn(dependentService1,'setPath');
spyOn(dependentService,'get');
  .....//other code

Now instead of using spyOn(dependentService1,'setPath') I could have done it like below :

var spyobj = createSpyObj('dependentService1',['setPath']); 

as well. So which is better? I believe I can pass spyObj as parameter as a mocked object to my controllers or the function under test. But would like to know any other advantage?

Aucun commentaire:

Enregistrer un commentaire