2015-04-30 203 views
1

我複製了從here進行單元測試的angularjs示例。由於它只是直接實現這個例子,所以我對所引發的錯誤感到困惑。Angularjs Jasmine單元測試

我在Linux中工作並使用括號作爲IDE。

請讓知道什麼是缺少的元素來運行茉莉花測試。

茉莉花的輸出繼電器

PasswordController encountered a declaration exception.  
ReferenceError: module is not defined 

controller.js

angular.module('app', []) 
.controller('PasswordController', function PasswordController($scope) { 
    $scope.password = ''; 
    $scope.grade = function() { 
    var size = $scope.password.length; 
    if (size > 8) { 
     $scope.strength = 'strong'; 
    } else if (size > 3) { 
     $scope.strength = 'medium'; 
    } else { 
     $scope.strength = 'weak'; 
    } 
    }; 
}); 

控制器spec.js

describe('PasswordController', function() { 
    beforeEach(module('app')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
    $controller = _$controller_; 
    })); 

    describe('$scope.grade', function() { 
    it('sets the strength to "strong" if the password length is >8 chars', function() { 
     var $scope = {}; 
     var controller = $controller('PasswordController', { $scope: $scope }); 
     $scope.password = 'longerthaneightchars'; 
     $scope.grade(); 
     expect($scope.strength).toEqual('strong'); 
    }); 
    }); 
}); 

卡瑪conf.js

// list of files/patterns to load in the browser 
    files: [ 
      'bower_components/angular/angular.js', 
      'bower_components/angular-mocks/angular-mocks.js', 
      'bower_components/angular-resource/angular-resource.js', 
      'app/controllers/*.js', 
      'test/controllers/*.js' 
    ], 

噶輸出

/var/www/html/angular-jasmine-testing $ kma start karma.conf.js 
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/ 
INFO [launcher]: Starting browser Chrome 
INFO [Chrome 41.0.2272 (Linux)]: Connected on socket vZCR4hAC8uU7LutFNVl3 with id 44440115 
Chrome 41.0.2272 (Linux): Executed 1 of 1 SUCCESS (0.042 secs/0.035 secs) 

Folder structure Image

+0

你確定你在你的項目中的所有這些文件?你可以發佈完整的業力輸出嗎? –

+0

Karma似乎成功地運行了測試:「執行了1個1成功」。 Brackets是否有可能試圖在Karma之外運行Jasmine規格? –

+0

我不認爲這個問題是與括號,因爲我跑了一個簡單的JavaScript測試,它得到執行。 –

回答

0

我設法讓你的測試工作。請參見下面的plunkr:

describe("PasswordController", function() { 
    beforeEach(module('app')); 

    var $scope = null; 

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

    describe("$scope.grade", function() { 
    it("sets the strength to 'strong' if the password length is >8 chars", function() { 
     $scope.password = "longerthaneightchars"; 
     $scope.grade(); 
     expect($scope.strength).toEqual("strong"); 
     }); 
    }) 

}); 

plunkr

+0

我試過你的代碼,它仍然會拋出錯誤。但業力被執行。可能是括號問題,如評論中所述。我會用崇高的測試,並保持你的發佈。 –

相關問題