-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.
、完璧な
さて、「API.newAccountInfo.update」はスパイではありません。エラーメッセージがきれいです – Phil
あなたのファクトリが毎回同じリソースを作成する関数を返すのはなぜですか?リソース自体を返してはいけませんか? – Phil
私は他のリソースのための他の機能が多すぎるので、ここで私はちょうど1つを言及します、そこにスパイを作成する方法はありませんか? –