-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 오류 : 나는 여기에 완벽한 코드가 작동

+0

글쎄, 'API.newAccountInfo.update'는 스파이가 아닙니다. 오류 메시지는 매우 명확합니다. – Phil

+0

팩토리가 매번 같은 리소스를 생성하는 함수를 반환하는 이유는 무엇입니까? 리소스 자체 만 반환하면 안됩니까? – Phil

+0

안녕하세요, 다른 리소스에 대한 다른 기능이 너무 많아서 여기에 하나만 언급했는데 스파이를 만들 수있는 방법이 없습니다. –

답변

0

단지 macke 공장을 오해 무엇

Expected a spy, but got undefined. 

직접 자원을 반환하고 기능을 제거합니다.

관련 문제