简体   繁体   中英

Javascript controller cannot be found by karma

I am attempting to test my node.js application using karma and jasmine frameworks. The issue I am facing is that the error below, saying that the name of my controller is not registered. I am trying to do a simple test to see if a scope variable is null.

Error:The controller with the name 'scheduleCtrl' is not registered.

I have been following the tutorials here link: https://docs.angularjs.org/guide/controller

scheduleController.js

 angular.module('myApp').controller('scheduleCtrl', 
    ['$scope', '$http', function($scope, $http){

     var indexOfHtml = window.
     $scope.selectedPatient = null;
  }

scheduleController_spec.js

describe("schedule Controller",function(){

    beforeEach(angular.module('myApp'));

    var $controller;
    var $scope = {}; 


    beforeEach(inject(function($rootScope, $controller){
            $scope = $rootScope.$new();
            //this creates a controller for us to use
            $controller('scheduleCtrl', { $scope: $scope});
    }));

            it(" should expect null ", function(){

                    expect($scope.selectedPatient).toEqual(null);

                    }); 

    //});

});

karma-config.js

   files: [

            './node_modules/angular/angular.js',
            './node_modules/angular-ui-router/release/angular-ui-router.js',
            './node_modules/angular-mocks/angular-mocks.js',

            './web/static/scripts/app.js',
            './web/static/views/home.html',
            './web/static/scripts/homeController.js',

            './web/static/scripts/scheduleController.js',

            './spec/spec_test.js'
            ],

Error Output

Chrome 56.0.2924 (Mac OS X 10.11.2) schedule Controller  should expect null  FAILED
TypeError: queueableFn.fn.call is not a function
Error: [$controller:ctrlreg] The controller with the name 'scheduleCtrl' is not registered.
http://errors.angularjs.org/1.6.2/$controller/ctrlreg?p0=scheduleCtrl
    at node_modules/angular/angular.js:68:12
    at $controller (node_modules/angular/angular.js:10690:17)
    at node_modules/angular-mocks/angular-mocks.js:2296:14
    at Object.<anonymous> (spec/scheduleController_spec.js:18:3)
    at Object.invoke (node_modules/angular/angular.js:4862:19)
    at Object.WorkFn (node_modules/angular-mocks/angular-mocks.js:3170:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:3133:25)
    at Suite.<anonymous> (spec/scheduleController_spec.js:15:13)
    at spec/scheduleController_spec.js:4:1
Expected undefined to equal null.
    at Object.<anonymous> (spec/scheduleController_spec.js:26:35)

Chrome 56.0.2924 (Mac OS X 10.11.2): Executed 1 of 1 (1 FAILED) ERROR (0.046 secs / 0.033 secs)

您可能希望将Karma basePath配置选项设置为应从中解析其他路径(例如files路径)的正确目录。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM