나는 ionic app을 썼다. 나는 그것에 문제를 직면하고있다 그것에 시험을 추가하기 시작했다 $resources
.성공 또는 오류시 약속을 처리하는 방법
angular.module('starter.services', [])
.factory('API', function($rootScope, $resource, $ionicPopup, $ionicLoading, $window) {
return {
newAccountInfo: $resource(base + '/restoreinfo/:restCode', {
restCode: '@_restCode'
}, {
update: {
method: 'PUT'
}
}, {
stripTrailingSlashes: false
});
}
과의 :
그리고 서비스
.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) {
$scope.comty = 'update';
$window.location.href = '#/login';
}, function(err) {
if (err.data == null)
$rootScope.popup("Error", "no connection");
else
$rootScope.popup('error', err.data.error);
});
}
})
$resources
를 사용하여 요청을이 경우 나는이 컨트롤러가 내 테스트는 다음 코드 :
,
describe('newAccountCtrl', function() {
var controller,
deferredLogup, window, scope,
$rootScope,
$q, store, API,
PROMISE = {
resolve: true,
reject: false
};
beforeEach(angular.mock.module('starter'));
beforeEach(module('starter.controllers'));
beforeEach(module('starter.services'));
beforeEach(module(function($provide, $urlRouterProvider) {
$provide.value('$ionicTemplateCache', function() {});
$urlRouterProvider.deferIntercept();
}));
beforeEach(inject(function($controller, _$rootScope_, $q, _API_, _$window_) {
$q = $q;
deferredLogup = $q.defer();
$rootScope = _$rootScope_;
spyOn($rootScope, 'popup')
scope = $rootScope.$new();
API = _API_;
window = _$window_;
spyOn(API.newAccountInfo, 'update').and.callThrough(function(callback) {
deferredLogup.promise.then(callback);
return deferredLogup.promise;
});
controller = $controller('newAccountCtrl', {
'$scope': scope,
'API': API,
'$window': window
});
}));
it('expect API.newAccountInfo to Have Been Called', function() {
scope.getloghist();
expect(API.newAccountInfo.upadate).toHaveBeenCalled();
})
it('on success ', function() {
scope.newInfo();
deferredLogup.resolve();
scope.$digest();
expect(scope.comty).toBeDefined();
});
it('on unsuccessful', function() {
scope.newInfo();
deferredLogup.reject();
scope.$digest()
expect($rootScope.popup).toHaveBeenCalled();
});
});
먼저 통과 기대하지만,
오류
에 반환 "성공"두 번째 "정의되지 않은 예상은 정의 할".
단위 테스트를 처음 사용했습니다. 내가 여기서 무엇을 놓치고 있니? 당신의 스파이에
안녕하세요, 저는 callTake를 callFake로 대체했으며 같은 오류가 나타납니다 ** "정의되지 않은 것으로 예상 됨"**. –
나는 내 대답을 편집했다. 그게 문제를 완전히 고쳐 줄지는 모르겠지만 좀 더 가까워 져야합니다. – jlogan
감사합니다 그 작품 :-) –