【问题标题】:Error initializing controller while testing angularjs with karma使用业力测试 angularjs 时初始化控制器时出错
【发布时间】:2014-07-17 08:31:46
【问题描述】:

我正在尝试使用 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

这些是我的文件:
你好.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 中。

还能是什么?我在网上没有找到太多关于这些错误的信息。我很高兴最终找到答案。

【问题讨论】:

  • 您使用的 angular.js 版本是什么?
  • 请确保 hello.js(真正的代码)确实包含在 karma.conf.js 中
  • 是的,这解决了它!非常感谢!

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多