【问题标题】:Running angular tests with karma and jasmine: minErr error使用 karma 和 jasmine 运行角度测试:minErr 错误
【发布时间】:2015-03-23 00:15:01
【问题描述】:

我正在尝试为角度控制器编写基本测试。我使用 Jasmine 框架和 karma 作为测试运行器。我的测试失败并出现以下错误:

Firefox 35.0.0 (Linux) MessageCtrl should get the correct message FAILED
    minErr/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:78:12
    loadModules/<@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3906:15
    forEach@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:325:9
    loadModules@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3872:5
    createInjector@/home/jenselme/tests/javascript/angular/unit_tests/angular.js:3812:11
    workFn@/home/jenselme/tests/javascript/angular/unit_tests/angular-mocks.js:2172:44
    env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
    createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
    [2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
    @http://localhost:9877/context.html:47:5

    TypeError: $scope is undefined in /home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js (line 11)
    @/home/jenselme/tests/javascript/angular/unit_tests/MessageCtrl.spec.js:11:5
    env.executeFiltered@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/boot.js:126:7
    createStartFn/<@/home/jenselme/.npm-packages/lib/node_modules/karma-jasmine/lib/adapter.js:171:5
    [2]</Karma/this.loaded@http://localhost:9877/karma.js:185:7
    @http://localhost:9877/context.html:47:5

这是我的 MessageCtrl.js:

var app = angular.module("lastLine", []);

app.controller('MessageCtrl', ['$scope', function($scope) {
    $scope.getMessage = function(name) {
    return 'Hello ' + name;
    };
}]);

还有我的 MessageCtrl.spec.js:

describe('MessageCtrl', function() {
    var $scope;

    beforeEach(module('MessageCtrl'));

    beforeEach(inject(function($rootScope, $controller) {
        $scope = $rootScope.$new();
        $controller('MessageCtrl', {
            $scope: $scope
        });
    }));

    it('should get the correct message', function() {
        var message = $scope.getMessage('Cedric');
        expect(message).toBe('Hello Cedric');
    })
});

我不知道这可能来自哪里。谁能帮帮我?

编辑1:更正MessageCtrl.spec.js的代码

【问题讨论】:

    标签: angularjs jasmine karma-runner


    【解决方案1】:

    知道了。 MessageCtrl.spec.js 中的代码是错误的。 beforeEach(module('MessageCtrl')); 应该是 beforeEach(module('lastLine'));

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多