2015-10-20 82 views
1

我無法理解如何建立與茉莉角,所以我可以做測試工作工作。我按照標題爲「測試控制器」的標題下面的說明here。根據該文件,你應該有你的應用程序&控制器,就像你通常會(這是從documenation粘貼)定義:麻煩與茉莉角

angular.module('app', []) 
.controller('PasswordController', function PasswordController($scope) { 
    //controller code goes here (removed for brevity) 
}); 

,然後你應該爲你的測試套件的代碼,例如(從粘貼文檔也是如此)。

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'); 
    }); 
    }); 
}); 

但我對一些事情感到非常困惑。

  1. 的文檔說明,你需要使用angular-mocks控制器加載,但在他們的榜樣,他們不申報ngMocks作爲一個應用程序的依賴(見第一個代碼塊我上面粘貼)。
  2. 它說,你可以使用angular.mock.inject控制器注入到當前上下文中。我加載了腳本http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-mocks.js,現在全球範圍內有一個angular.mock,但它沒有inject方法。此外,由於測試代碼在控制器之外運行,因此我不明白在角度應用中如何使用依賴項有助於爲注入控制器提供全局方法。整件事對我來說沒有意義。
  3. 同樣適用module。它說你可以將它用於beforeEach(module('app'));,並且它由angular-mocks提供,但angular.mock沒有module方法。

如果有人能說明什麼我做錯了我會非常感激!

回答

0

所以我發現,問題是,我對angular-mocks腳本標記爲以前茉莉我的腳本標籤時,它真正需要後去。在Angular的「文檔」的典型精神中,這一點沒有提及。重新排列腳本標記後,moduleinject都是全局可用的方法。

因此,要回答我的第一個問題,你不需要把ngMock的依賴關係。這回答了問題2和3,因爲moduleinject現在都是全球可用的。

因此腳本需要放置順序。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css"> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.js"></script> 
<!--angluar mocks script MUST go after the other declarations otherwise it won't add the inject and module methods to the scope --> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-mocks.js"></script>