spyOn功能无法正常工作

时间:2014-06-18 19:26:48

标签: angularjs jasmine

我的控制器执行如下操作:

.controller('demo',function($scope,$state, $modal,$q, Resource1, Resource2) {

  function1(){
     var deferred = $q.defer();
     Resource1.get(function(result){
         $scope.player = result;
         deferred.resolve(promise);
     });
     return deferred.promise;
  }  

  function2() { /* similar to function1. It also returns a promise */ }

   $q.all(function1(),function2(),function(result){

   });

});

我已经把自己推向了...除此之外,我有点失落。首先我想知道我是否正确地初始化事物,其次是如何编写一个单元测试来测试function1()被调用以及之后是否调用了Resource1。这就是我到目前为止所做的:

describe('demo',function(){


  beforeEach(function(){
      $rootScope = $injector.get('$rootScope');
      $controller = $injector.get('$controller');
      $httpBackend = $injector.get('$httpBackend');
      q = $injector.get('$q');
      state = $injector.get('$state');
   });

  createController = function(){
      return $controller('demo',{
        $scope: $rootScope.$new(),
        $state: state,
        $modal: sinon.stub({}),
        $q: q,
        Resource1: mockResource1, /*how and where to initialize mock Resources */
        Resource2: mockResource2, 

      });

    } //end beforeEach

   it('should check that func1 was called',function(){
          spyOn(this,'function1');
          var controller = createController();
          expect(this.function1).toHaveBeenCalled();
    });

   it('should check that Resource1.get as called',function(){
          //  how to formulate the test here 
    });

});

1 个答案:

答案 0 :(得分:0)

我认为function1是一个存在于控制器范围内的函数。你不能“触摸”它,所以你不能“窥探”它。