TypeError:无法读取undefined的属性

时间:2015-07-07 20:27:13

标签: angularjs angularjs-scope jasmine

有一些范围挣扎: 我想用Jasmine在控制器中测试我的shuffle函数。 控制器:

(function(){
   'use strict';
   angular.module('flickrNgSpaApp').controller('discoverController', ['$scope', '$http', function($scope, $http ){

     $scope.shuffle = function(array) {
     var counter = array.length, temp, index;
     while (counter > 0) {
       index = Math.floor(Math.random() * counter);
       counter--;
       temp = array[counter];
       array[counter] = array[index];
       array[index] = temp;
     }
     return array;
   };
   $scope.shuffled=$scope.shuffle(array);
  ...  
})();

和我的茉莉花测试:

'use strict';

var scope, $controller, $rootScope;
beforeEach(module('flickrNgSpaApp'));
describe('Controller: discoverController', function () {
  beforeEach(inject(function (_$controller_) {
    $controller = _$controller_;
  }));

  describe('shuffle array of tags', function () {
    it('Should shuffle tags', function () {
      var $scope = {};
      var controller = $controller('discoverController', {
        $scope: $scope
      });
      var tags = ['tag1', 'tag2', 'tag3'];
      var shuffledTags = scope.shuffle(tags);
      expect(shuffledTags).not.toEqual(['tag1', 'tag2', 'tag3']);
    });
  });

我遇到了TypeError:无法读取属性' shuffle'未定义的。 有什么想法吗?

0 个答案:

没有答案