2016-01-31 2 views
0

Я создал приложение календаря, и я пытаюсь создать модульный тест, чтобы показать, где 1 января 2016 будет отображаться на массиве календаря, который должен бытьAngularJS Test Unit Массив с кармой

$scope.weeks[0][5]

, Тем не менее, когда я тестирую свой массив, я продолжаю получать неопределенный элемент в первом элементе массива.

Здесь ошибка:

TypeError: Cannot read property '0' of undefined

Не знаете, где я неправильно. Любая помощь будет оценена по достоинству.

Вот мой блок код теста:

describe('correctDate', function(){ 
    var $scope, ctrl; 
    beforeEach(module("calendarDemoApp")); 
    beforeEach(inject(function($controller, $rootScope){ 
     $scope = $rootScope.$new(); 
     ctrl = $controller('MonthCtrl', { 
      $scope: $scope 
     }); 
     $scope.date.month = 0; 
     $scope.date.day = 1; 
     $scope.date.year = 2016; 
     $scope.$apply(); 
    })); 
    fit('should be the 5th element in array', function(){ 
     expect($scope.date.day).toBe($scope.weeks[0][5].day); 
     expect($scope.date.month).toBe($scope.weeks[0][5].month); 
     expect($scope.date.year).toBe($scope.weeks[0][5].year); 
    }); 
}); 

Вот код из моего приложения:.

angular.module('calendarDemoApp', []) 
    .controller('MonthCtrl', function($scope){ 
     $scope.$watch('date', function(newDate){ 
      var range = CalendarRange.getMonthlyRange(new Date(newDate.year, newDate.month, 1)); 
      var totalWeeks = range.days.length/7;   
      var weeks = []; 
      for(i=0; i<totalWeeks; i++){ 
       weeks[i] = []; 
       for(j=0; j<7; j++){ 
        weeks[i].push(range.days[j+(i*7)]); 
       } 
      } 
      $scope.weeks = weeks; 
     }, true); 
}) 

ответ

2
describe('correctDate', function(){ 
    var $scope, ctrl; 
    beforeEach(module("calendarDemoApp")); 
    beforeEach(inject(function($controller, $rootScope){ 
     $scope = $rootScope.$new(); 
     ctrl = $controller('MonthCtrl', { 
      $scope: $scope 
     }); 
     $scope.date.month = 0; 
     $scope.date.day = 1; 
     $scope.date.year = 2016; 
     $scope.$apply(); 
    })); 
    fit('should be the 5th element in array', function(){ 
     expect($scope.date.day).toBe($scope.weeks[0][5].day); 
     expect($scope.date.month).toBe($scope.weeks[0][5].month); 
     expect($scope.date.year).toBe($scope.weeks[0][5].year); 
    }); 
}); 

Забыл добавить $ объема $ применяется() для $ часов стрелять. Также были указаны мои параметры в неправильном порядке. Если бы это:

var range = CalendarRange.getMonthlyRange(new Date(newDate.month, newDate.year, 1));

Когда это должно было быть так:

var range = CalendarRange.getMonthlyRange(new Date(newDate.year, newDate.month, 1));

Он установил год в месяц 1, а затем в месяц 2016. Давать мне этот расчет: 2016/12 = 168; 1900 + 168 = 2068.

Смежные вопросы