Angular mock找不到我的控制器,不能用Karma和require下的grunt运行js单元测试

时间:2015-02-28 01:32:44

标签: angularjs requirejs karma-mocha

以下是我在Git Hub中的示例代码:

https://github.com/Mellon/KarmaWithRequirejs.git

运行" grunt test"时,我收到此错误。

Error: [ng:areq] Argument 'MyController' is not a function, got undefined
http://errors.angularjs.org/1.3.14/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined
    at assertArg (/Users/csun/FrontEnd/KarmaWithRequirejs/node_modules/angular/angular.js:1580)
    at assertArgFn (/Users/csun/FrontEnd/KarmaWithRequirejs/node_modules/angular/angular.js:1591)
    at /Users/csun/FrontEnd/KarmaWithRequirejs/node_modules/angular/angular.js:8431
    at /Users/csun/FrontEnd/KarmaWithRequirejs/test/myControllerSpec.js:13
    at invoke (/Users/csun/FrontEnd/KarmaWithRequirejs/node_modules/angular/angular.js:4185)
    at workFn (/Users/csun/FrontEnd/KarmaWithRequirejs/node_modules/angular-mocks/angular-mocks.js:2364)
undefined

1 个答案:

答案 0 :(得分:0)

define(['expect'], function () {
    'use strict';
    describe('MyController', function () {
        var controllerContructor, scope, controller;
        before(function () {//change before to beforeEach, it works.
            module('app');
            inject(function ($controller, $rootScope) {
                controllerContructor = $controller;
                scope = $rootScope.$new();
                controller = controllerContructor('MyController', {$scope: scope});
            });
        });

        it('shoule be loaded.', function () {
            expect(controller).to.be.an('object');
        });
    });
});