在使用karma测试angularjs时初始化控制器时出错

时间:2014-07-17 08:31:47

标签: javascript angularjs unit-testing controller karma-runner

我正在尝试使用Karma测试我的应用程序,但我得到以下错误:

minErr/<@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:78:5
loadModules/<@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3859:1
forEach@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:325:7
loadModules@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3824:5
createInjector@/home/usr/webui-ng/src/client/app/bower_components/angular/angular.js:3764:3
workFn@/home/usr/webui-ng/src/client/app/bower_components/angular-mocks/angular-mocks.js:2150:9

这些是我的档案:
hello.js

angular.module('myApp', [])
.controller('MainController', function($scope) {
    $scope.name = "Ari";
    $scope.sayHello = function() {
        $scope.greeting = "Hello " + $scope.name;
    }
})

hello.js - 测试文件:

describe('Unit: MainController', function() {
// Load the module with MainController
beforeEach(module('myApp'));

var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
    // Create a new scope that's a child of the $rootScope
    scope = $rootScope.$new();
    // Create the controller
    ctrl = $controller('MainController', {
        $scope: scope
    });
}));

it('should create $scope.greeting when calling sayHello',
    function() {
        expect(scope.greeting).toBeUndefined();
        scope.sayHello();
        expect(scope.greeting).toEqual("Hello Ari");
    });

});

如您所见,我已加载模块,因为解决方案位于Controller undeclared in Jasmine test

它还能做什么?我在网上找不到这些错误。我很高兴终于找到答案。

1 个答案:

答案 0 :(得分:1)

正如runTarm所提到的,karma.conf.js中的路径未设置为脚本文件(hello.js)的实际路径