Ho il seguente controller ViewMeetingCtrl.jsspiare una chiamata di metodo di servizio utilizzando Spies gelsomino
(function() {
'use strict';
angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl);
ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService'];
function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) {
$scope.meeting = meeting;
$scope.cancelMeeting = cancelMeeting;
function cancelMeeting(meetingId, companyId) {
meetingService.sendCancelNotices(companyId, meetingId)
.success(function() {
$state.go('company.view');
});
}
}
})();
sono stato in grado di richiamare l'succussfully spyOn per cancelMeeting() ma non con la chiamata di sendCancelNotices metodo. Quello che voglio fare è che voglio testarlo ogni volta che cancelMeeting() viene chiamato, chiama il metodo sendCancelNotices(). So che dovrei andare con il metodo createSpy per farlo. Ma non sono sicuro di come farlo.
Di seguito si riporta il caso di test ViewMeetingCtrlSpec.js
describe('ViewMeetingCtrl CreateSpy --> Spying --> cancelMeeting', function() {
var $rootScope, scope, $controller , $q ;
var sendCancelNoticesSpy = jasmine.createSpy('sendCancelNoticesSpy');
beforeEach(angular.mock.module('MyApp'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
createController = function() {
return $controller('ViewMeetingCtrl', {
$scope: scope,
meeting : {}
});
};
var controller = new createController();
}));
it("tracks that the cancelMeeting spy was called", function() {
//some assertion
});
});
effettivamente solo bisogno di utilizzare il metodo spyOn come spyOn (servizio, 'metodo') http: //jasmine.github .io/2.0/introduction.html che invocherà il metodo originale dopo aver eseguito le operazioni relative alla spia –