TypeError:$ controller不是函数 - angularjs jasmine

时间:2016-09-18 17:21:57

标签: angularjs unit-testing karma-jasmine

我正在尝试使用Jasmin / Karma测试我的AngularJS代码。我也喜欢使用MeanJS。 当我启动测试时,出现以下错误

'use strict';

angular.module('myapp', [])
    .controller('MyCtrl', ['$scope', function MyCtrl($scope) {
        $scope.greeting = "hello";
    }]);

describe('My controller', function() {
    var $controller, $scope;

    beforeEach(module('myapp'));

    beforeEach(inject(function($rootScope, _$controller_) {
        $controller = _$controller_;
        $scope = $rootScope.$new();
    }));

    it('greets', function() {
        var controller = $controller('MyCtrl', {
            $scope: $scope
        });
        expect($scope.greeting).toEqual('hello');
    })

});

以下是不起作用的示例:

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 53.0.2785 (Linux 0.0.0)]: Connected on socket 8e4tfQKvty6CbglWSqji with id 23318510
Chrome 53.0.2785 (Linux 0.0.0) My controller greets FAILED
        Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=ngMock&p1=Error%3A%20%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.5.0%2F%24injector%2Funpr%3Fp0%3D%2524%2524asyncCallbackProvider%0A%20%20%20%20at%20Error%20(native)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A6%3A416%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A43%3A7%0A%20%20%20%20at%20Object.d%20%5Bas%20get%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A40%3A270)%0A%20%20%20%20at%20Object.decorator%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A42%3A339)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular-mocks%2Fangular-mocks.js%3F9e9911cea3845a1d5dec76f3cf2dfaa60fd9c669%3A1947%3A12%0A%20%20%20%20at%20Object.invoke%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A41%3A295)%0A%20%20%20%20at%20d%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A39%3A234)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A39%3A358%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fpublic%2Flib%2Fangular%2Fangular.min.js%3F0ff025e7c37c0d56654bcf2cc4de18ea50ead3b3%3A7%3A355)
            at Error (native)
            at /home/adrien/Projects/Codowl/Beta-Site/public/lib/angular/angular.min.js:6:416
            at /home/adrien/Projects/Codowl/Beta-Site/public/lib/angular/angular.min.js:40:60
            at n (/home/adrien/Projects/Codowl/Beta-Site/public/lib/angular/angular.min.js:7:355)
            at g (/home/adrien/Projects/Codowl/Beta-Site/public/lib/angular/angular.min.js:39:135)
            at Object.fb [as injector] (/home/adrien/Projects/Codowl/Beta-Site/public/lib/angular/angular.min.js:43:164)
            at Object.workFn (/home/adrien/Projects/Codowl/Beta-Site/public/lib/angular-mocks/angular-mocks.js:2464:52)
        TypeError: $controller is not a function
            at Object.<anonymous> (/home/adrien/Projects/Codowl/Beta-Site/public/modules/users/tests/account-creation.spec.js:21:20)
Chrome 53.0.2785 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.017 secs / 0.015 secs)
Warning: Task "karma:unit" failed. Used --force, continuing.

这是完整的错误:

module mod {
    yang-version 1;
    namespace "http://example.com/mod";
    prefix m;
    revision "2016-09-09" {
       description "Initial revision.";
    }
    container foo {
        description 'container foo';
        leaf l {
            type string;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为我的角度模拟与我的凉亭文件中的角度依赖性版本不同。 这很难:'D