簡體   English   中英

使用業力和茉莉花運行角度測試:minErr錯誤

[英]Running angular tests with karma and jasmine: minErr error

我正在嘗試為角度控制器編寫基本測試。 我將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的代碼

得到它了。 MessageCtrl.spec.js中的代碼錯誤。 beforeEach(module('MessageCtrl')); 應該是beforeEach(module('lastLine'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM