mercredi 4 mai 2016

Testing angular factory

I'm trying hard to bring up the unit test level of an Angular project but struggling to test an AngularJS factory that has dependencies.

My factory has 5 dependencies but I can't even get a test working that just tests that the factory is defined. Here is some sample code of what I'm doing. I have tried using inject, $provide ect but I keep getting an error along the lines of

"Configuration parameter should be an object, instead it is a: undefined"

Here's the test, so far, I've left commented out code in to show what I've been trying:

(function() {

'use strict';

 describe('While working with the channelService', function(){

var sut,
    q,
    filter,
    common,
    dataContext,
    etCommon;

beforeEach(function(){

  // Load the module
  //  module('app');
   module('app');
   module('app.inventory');
   module('blocks.router');

  inject(function($injector){
    sut = $injector.get('channelService');
    q = $injector.get('$q');
    filter = $injector.get('$filter');
    common = $injector.get('common');
    dataContext = $injector.get('dataContext');
    etCommon = $injector.get('etCommons');

    spyOn(q, 'defer');
    spyOn(filter  , 'defer');
    spyOn(common, 'makeStruct');
    spyOn(q, 'defer');
    spyOn(q, 'defer');
  });

  // inject(function($injector){
  //     sut = $injector.get('channelService');
  //
  //     var test = 'some text';
  // })

  // inject(function(_channelService_, _$q_, _$filter_, _common_, _dataContext_, _etCommon_){
  //   sut = _channelService_;
  //   q = _$q_;
  //   filter = _$filter_;
  //   common = _common_;
  //   dataContext = _dataContext_;
  //   etCommon = _etCommon_;
  // });

  // spyOn(q, 'defer');
});

// beforeEach(function(){
//
//   q = {
//     defer: function(){ return deferred.promise;},
//     when: function(){},
//     then: function(){}
//   };
//   filter = {};
//   common = {};
//   dataContext = {};
//   etCommon = {};
//
//   module(function($provide){
//     $provide.value('$q', q);
//     $provide.value('$filter', filter);
//     $provide.value('common', common);
//     $provide.value('dataContext', dataContext);
//     $provide.value('etCommon', etCommon);
//   })
//
//   spyOn(q, 'defer');
// });

describe('should define the following publicly', function(){
  it('should be defined itself', function(){
      expect(sut).toBeDefined();
  });
});

});

})();

Aucun commentaire:

Enregistrer un commentaire