2013-04-03 45 views
5

Tôi chỉ mới bắt đầu với góc cạnh và tôi muốn viết một số xét nghiệm đơn vị đơn giản cho bộ điều khiển của tôi, đây là những gì tôi nhận được.angularjs - bộ kiểm tra kiểm tra

app.js:

'use strict'; 


// Declare app level module which depends on filters, and services 
angular.module('Prototype', ['setsAndCollectionsService']). 
    config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/dashboard', {templateUrl: 'partials/dashboard.html', controller: 'DashboardController'}); 
    $routeProvider.when('/setsAndCollections', {templateUrl: 'partials/setsAndCollections.html', controller: SetsAndCollectionsController}); 
    $routeProvider.when('/repetition', {templateUrl: 'partials/repetition.html', controller: RepetitionController}); 
    $routeProvider.otherwise({redirectTo: '/dashboard'}); 
    }]); 

và controllers.js

'use strict'; 

/* Controllers */ 

    var myApp = angular.module('Prototype'); 

    myApp.controller('DashboardController', ['$scope', function (scope) { 
     scope.repeats = 6; 
    }]); 

/*function DashboardController($scope) { 
$scope.repeats = 5; 
};*/ 

function SetsAndCollectionsController($scope, $location, collectionsService, repetitionService) { 
    $scope.id = 3; 
    $scope.collections = collectionsService.getCollections(); 
    $scope.selectedCollection; 
    $scope.repetitionService = repetitionService; 

    $scope.switchCollection = function (collection) { 
     $scope.selectedCollection = collection; 
    }; 

    $scope.addCollection = function() { 
     $scope.collections.push({ 
      name: "collection" + $scope.id, 
      sets: [] 
     }); 
     ++($scope.id); 
    }; 

    $scope.addSet = function() { 
     $scope.selectedCollection.sets.push({ 
      name: "set" + $scope.id, 
      questions: [] 
     }); 
     ++($scope.id); 
    }; 

    $scope.modifyRepetition = function (set) { 
     if (set.isSelected) { 
      $scope.repetitionService.removeSet(set); 
     } else { 
      $scope.repetitionService.addSet(set); 
     } 

     set.isSelected = !set.isSelected; 
    }; 

    $scope.selectAllSets = function() { 
     var selectedCollectionSets = $scope.selectedCollection.sets; 

     for (var set in selectedCollectionSets) { 
      if (selectedCollectionSets[set].isSelected == false) { 
       $scope.repetitionService.addSet(set); 
      } 
      selectedCollectionSets[set].isSelected = true; 
     } 
    }; 

    $scope.deselectAllSets = function() { 
     var selectedCollectionSets = $scope.selectedCollection.sets; 

     for (var set in selectedCollectionSets) { 
      if (selectedCollectionSets[set].isSelected) { 
       $scope.repetitionService.removeSet(set); 
      } 
      selectedCollectionSets[set].isSelected = false; 
     } 
    }; 

    $scope.startRepetition = function() { 
     $location.path("/repetition"); 
    }; 
} 

function RepetitionController($scope, $location, repetitionService) { 
    $scope.sets = repetitionService.getSets(); 
    $scope.questionsLeft = $scope.sets.length; 
    $scope.questionsAnswered = 0; 
    $scope.percentageLeft = ($scope.questionsLeft == 0 ? 100 : 0); 

    $scope.endRepetition = function() { 
     $location.path("/setsAndCollections"); 
    }; 
} 

bây giờ tôi đang ở quá trình chuyển đổi bộ điều khiển chức năng toàn cầu cho những người được xác định bởi API góc như bạn có thể nhìn thấy bằng cách ví dụ về DashboardController .

Bây giờ trong thử nghiệm của tôi:

describe("DashboardController", function() { 
    var ctrl, scope; 

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

    it("has repeats attribute set to 5", function() { 
     expect(scope.repeats).toBe(5); 
    }); 
}); 

Tôi nhận

Error: Argument 'DashboardController' is not a function, got undefined 

tôi tự hỏi sau đó, đâu là sai lầm của tôi? Nếu tôi hiểu điều này đúng, ctrl = $controller('DashboardController', {$scope: scope}); nên tiêm phạm vi mới được tạo của tôi vào số DashboardController để điền vào nó với các thuộc tính - trong trường hợp này là repeats.

+0

Bạn có chắc chắn bao gồm controller.js trong thử nghiệm không? Ví dụ trong tập tin cấu hình nghiệp của bạn? – Xesued

+0

Tôi đã kiểm tra, tập tin được đọc OK, điều thú vị là, nếu tôi bỏ ghi chú kiểu chức năng toàn cục của 'Bảng điều khiểnController', kiểm tra đơn giản này sẽ đi qua để tập tin này được đưa vào. – Andna

+3

Đối với những gì nó đáng giá bạn không nên treo các yếu tố ra khỏi biến phạm vi $ của bạn. $ scope phải chứa REFERENCE cho mô hình của bạn, nhưng nó KHÔNG phải là mô hình của bạn. ví dụ. '$ scope.id' phải giống như' $ scope.SetsAndCollectionsModel.id'. Lý do cho điều này xuất phát từ các vấn đề cố gắng thiết lập các kiểu nguyên thủy từ một phạm vi con đến phạm vi cha mẹ. Bạn có thể xem video mô tả điều này [ở đây] (http://www.egghead.io/video/DTx23w4z6Kc). Misko, người đã tạo ra Angular, cũng nói về nó trong một video [thực hành tốt nhất] (http://www.youtube.com/watch?v=ZhfUv0spHCY) (xin lỗi, không có liên kết trực tiếp đến thời điểm đó) –

Trả lời

19

Trước tiên, bạn cần phải thiết lập mô-đun Prototype của mình.

beforeEach(module('Prototype')); 

Thêm vào thử nghiệm của bạn, trên beforeEach hiện tại sẽ hoạt động.

describe("DashboardController", function() { 
    var ctrl, scope; 

    beforeEach(module('Prototype')); 

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

    it("has repeats attribute set to 5", function() { 
    expect(scope.repeats).toBe(5); 
    }); 
}); 
+0

Cảm ơn, đó là mảnh còn thiếu. – Andna

Các vấn đề liên quan