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