-1

我做離子應用程序,它完成,但是當我開始測試增加了它,我面臨着一個問題,$資源,在這種情況下,我有這樣的控制器:單元測試控制器

.controller('newAccountCtrl', function($scope, $window, $rootScope, API, $ionicPopup, $state) { 
    $scope.newData = {}; 
    $scope.$on('$ionicView.enter', function() { 

     $scope.newData = {}; 
    }); 
    $scope.newInfo = function() { 
     API.newAccountInfo().update({ restCode: $scope.newData.restore_code }, $scope.newData, function(res, header) { 
      $rootScope.popup('success', "OKAY"); 
      $window.location.href = '#/login'; 
     }, function(err) { 
      if (err.data == null) 
       $rootScope.popup("Error", "no connection"); 
      else 
       $rootScope.popup('error', err.data.error); 
     }); 
    } 
}) 

,並在服務我使功能使用$資源的請求:

angular.module('starter.services', []) 
.factory('API', function($rootScope, $resource, $ionicPopup, $ionicLoading, $window) { return { 
      newAccountInfo: function() { 
      return $resource(base + '/restoreinfo/:restCode', { restCode: '@_restCode' }, { 
       update: { 
        method: 'PUT' 
       } 
      }, { 
       stripTrailingSlashes: false 
      }); 
     }}}); 

,並在我的測試下面的代碼:

describe('newAccountCtrl', function() { 

var controller, 
    deferredLogup, scope, $q; 
beforeEach(angular.mock.module('starter')); 
// TODO: Load the App Module 
beforeEach(module('starter.controllers')); 
beforeEach(module('starter.services')); 

// TODO: Instantiate the Controller and Mocks 
beforeEach(inject(function($controller, _$q_, $rootScope, _API_) { 
    $q = _$q_; 
    scope = $rootScope.$new(); 
    API = _API_; 

    spyOn(API, 'newAccountInfo').and.callThrough(function(callback) { 
     deferredLogup.promise.then(callback); 
     return { $promise: deferredLogup.promise }; 
    }); 

    controller = $controller('newAccountCtrl', { 
     '$scope': scope, 
     API: API 
    }); 

})); 
it('#newAccountInfo', function() { 

    scope.newInfo(); 

    expect(API.newAccountInfo.update).toHaveBeenCalled(); 

}) }); 

,但我得到個E錯誤:

Expected a spy, but got undefined. 

我誤會這裏,代碼工作完美

+0

好吧,'API.newAccountInfo.update'不是間諜。錯誤消息非常明確 – Phil

+0

爲什麼您的工廠每次都會返回一個創建相同資源的函數?它不應該只是返回資源本身嗎? – Phil

+0

你好,因爲我對其他資源有太多其他功能,這裏我只提一個,有沒有辦法在它上面創建一個間諜? –

回答

0

只是MACKE廠返回資源的直接和刪除功能。