jeudi 3 mars 2016

jasimine argument scope is required when instantiating a controller

I am working to initiate a controller sits inside a directive. I have some tests I need to run but right now I am not able to access the controller with ng-Mock.

describe('hero Directive', function () {
var $compile,
    $rootScope,
    $scope,
    element,
    ctrl;

beforeEach(function () {
    angular.mock.module('ha.module.core');

    angular.mock.inject(function (_$compile_, _$rootScope_, _$controller_, $templateCache) {


        $compile = _$compile_;
        element = angular.element("<div exlore-hereo></div");
        $compile(element)($rootScope);
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        ctrl = _$controller_('ExploreHeroController', { $scope: $scope });
        console.log(ctrl)


        $scope.$digest();
    });

});

afterEach(function () {
    // need to remove the element element.remove();
});

describe('directive controller', function () {
    it('should dispatch call $emit with $methodsBound', function () {
        //spyOn($scope, '$emit');
        spyOn($scope, 'ControllerName');


        //expect(scope.$emit).toHaveBeenCalledWith('$methodsBound');
        //expect(ctrl).toHaveBeenCalled();
    });
});

});

I created an element compiled it and called the $digest method.

The error that I got was

  Argument 'scope' is required.   

So I tried spying on the it with jasmine

spyON($scope, 'ControllerName');

My controller inside of my directive is pretty basic.

var ControllerName = function($scope) {
   $scope.$emit('$method');
}

It seems like I need a spy, but I am not sure why the one I created does not work.

Aucun commentaire:

Enregistrer un commentaire