AngularJS单元测试:模块' admin.module'不可用

时间:2016-11-10 12:06:23

标签: javascript angularjs unit-testing visual-studio-2015 karma-jasmine

我正在使用Karma和Jasmine测试一个Angular控制器,但我似乎无法从我的主类中加载我的模块。

这是我的主要课程:admin.controller.js

angular.module('admin.module').controller('admin.controller', ['$scope', function ($scope) {
$scope.SaveChanges = function()
{
    return true;
}
}]);

这是我的考试班:admin.controller.tests.js

describe('admin.controller tests', function () {

    beforeEach(module('admin.module'));
    var $controller = {};

    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));

    describe('$scope.SaveChanges', function () {
        it('Should return true', function () {
            var $scope = {};
            var controller = $controller('admin.controller', { $scope: $scope });
            expect($scope.SaveChanges()).toBe(true);
        });
    });
});

我的karma.conf.js文件指向我项目中的以下文件:

// list of files / patterns to load in the browser
files: [
  '../TriAngular/scripts/angular.js',
  '../TriAngular/scripts/angular-mocks.js',
  '../TriAngular/app/admin/*.js',
  'app/admin/*.js'
],

admin.controller.js文件位于../TriAngular/app/admin内,我的admin.controller.test.js位于app / admin'内。

我试图直接引用我的karma配置文件中没有效果的文件。完整的错误是:

  

模块' admin.module'不可用!你要么拼错了   模块名称或忘记加载它。如果注册模块,请确保   您将依赖项指定为第二个参数。

2 个答案:

答案 0 :(得分:1)

问题证明并非显而易见。我错过了一个angular-route.js文件,需要包含它,因为它看起来像我的管理模块依赖它。

我的karma.conf.js文件中的包含列表:

// list of files / patterns to load in the browser
files: [
  '../TriAngular/scripts/angular.js',
  '../TriAngular/scripts/angular-mocks.js',
  '../TriAngular/scripts/angular-route.js',
  '../TriAngular/app/admin/*.js',
  'app/admin/*.js'
],

答案 1 :(得分:0)

尝试;

function calculateMinutes(DateInterval $int){
    $days = $int->format('%a');
    return ($days * 24 * 60) + ($int->h * 60) + $int->i;
}

我不熟悉Karma,但你也需要注册你的模块,我只使用了Jasmine和resharper,所以我做的就是注册文件;

beforeEach(function(){
module('admin.module');
    }
});

位于文件顶部。

另外也不要忘记引用模块的依赖项。