基本角度测试

时间:2014-08-01 18:45:43

标签: angularjs jasmine

我对JavaScript& Angular和我正在尝试测试这个控制器,但我在这里遗漏了一些东西。这是错误:

PhantomJS 1.9.7 (Mac OS X) Controller: UserCalendarCtrl Calendar resize based on width resize should call mobile FAILED
        spyOn could not find an object to spy upon for width()

用户日历controller.spec.js

'use strict';

describe('Controller: UserCalendarCtrl', function() {

  var controller,
    mockScope,
    VivaCalendar;

  beforeEach(module('vivaAngularApp', 'ui.calendar'));

  beforeEach(inject(function($controller, $rootScope, _VivaCalendar_) {
    mockScope = $rootScope.$new();
    controller = $controller('UserCalendarCtrl', {
      $scope: mockScope
    });
    VivaCalendar = _VivaCalendar_;
  }));

  describe('Calendar resize based on width', function() {

    it('should set a width', function() {
      expect(mockScope.width).toBeDefined(true);
    });

    describe('resize', function() {

      beforeEach(function (mockScope){
        spyOn(mockScope, 'width').and.callFake(function(){
          return 700;
        });
      });

      it('should call mobile', function() {
        expect(mockScope.uiConfig).toEqual(VivaCalendar.uiConfig('mobile'));
      });
    });
  });

  describe('event sources', function(){
    it('should return event sources', function() {
      expect(mockScope.eventSources).toEqual([mockScope.eventSource]);
    });

    it('should take event source', function(){
      expect(mockScope.eventSource.url).toEqual('http://162.243.222.54/fullcalendar/new_fechas_insp.php');
    });
  });
});

用户日历controller.js

'use strict';

app.controller('UserCalendarCtrl', ['$scope', '$window', '$location', 'VivaCalendar', function($scope, $window, VivaCalendar, $location) {

  $scope.width = $window.innerWidth;

  $scope.$watch('width', function(width) {
    if (width < 768) {
      $scope.uiConfig = VivaCalendar.uiConfig('mobile');
    } else {
      $scope.uiConfig = VivaCalendar.uiConfig();
    }
  });

  $scope.eventSource = {
    url: 'http://162.243.222.54/fullcalendar/new_fechas_insp.php'
  };

  $scope.eventSources = [$scope.eventSource];
}]);

VIVA-calendar.js

'use strict';

app.service('VivaCalendar', function() {

  var UiConfig = function(size) {
    this.calendar = {
      defaultView: 'month',
      allDaySlot: false,
      minTime: 8,
      maxTime: 22,
      timeFormat: {
        agenda: 'h:mm{ - h:mm}TT'
      },
      header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek'
      },
      buttonText: {
        prev:     '&lsaquo;', // <
        next:     '&rsaquo;', // >
        prevYear: '&laquo;',  // <<
        nextYear: '&raquo;',  // >>
        today:    'Ver el día de hoy',
        month:    'mes',
        week:     'semana',
        day:      'día'
      },
      columnFormat: {
        month: 'ddd',    // Mon
        week: 'ddd d/M', // 9/7 Mon
        day: 'dddd M/d'  // 9/7 Monday 
      },
      monthNamesShort: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
      dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],
      dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
      editable: false,
      hiddenDays: [0, 6]
    };

    if (size === 'mobile') {
      this.calendar.defaultView = 'agendaDay';
      this.calendar.header.right = 'month, agendaDay';
    }
  };

  this.uiConfig = function(size) {
    return new UiConfig(size);
  };
});

1 个答案:

答案 0 :(得分:2)

间谍的目标是用记录函数调用的另一个函数替换函数,并且可能执行除被替换函数之外的其他操作。

width这里不是一个函数。它是类型编号的简单属性。所以窥探它是没有意义的。如果您希望范围的宽度为700,则只需

mockScope.width = 700;