使用业力和茉莉花进行角度测试:minErr错误

时间:2015-01-23 21:02:11

标签: angularjs jasmine karma-runner

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

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的代码

1 个答案:

答案 0 :(得分:0)

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