samedi 27 février 2016

How can I make successful unit test?

I am trying to write a unit test for my custom directive:

.directive('uiList', [
    function(scriptingService) {
      return {
        scope: {
          lengthModel: '=uiList'
        },
        link: function(scope, elm, attrs) {
          scope.$watch('lengthModel', function() {
            scriptingService.getScript(request).then(function(scripts) {
              //something
            });
          });
        }
      };
    }

Inside I call a service:

.service('scriptingService', function() {
    var scriptingService = {
      getScript: function() {
        return 'blaat';
      }
    };

    return scriptingService;
  })

I would like to test whether the getScript method is called so I wrote this test;

beforeEach(inject(function($rootScope, $compile, _scriptingService_) {
    scope = $rootScope.$new();
    scope.row = 1;

    scriptingService = _scriptingService_;
    spyOn(scriptingService, 'getScript');
    element = angular.element('<ul id="rows" ui-list="row">');
    $compile(element)(scope);
    scope.$digest();
    elementScope = element.scope();
  }));

  it('should call service', function() {


    scope.$apply(function() {
      scope.row = 2;
    });

    expect(scriptingService.getScript).toHaveBeenCalled();

  });

At the moment I get an error: TypeError: Cannot read property 'getScript' of undefined. Why do I get this error and how can I fix it? I thought I mocked the service out already?

plunkr: http://ift.tt/1LMUCpC

Aucun commentaire:

Enregistrer un commentaire