间谍使用茉莉间谍服务的方法调用 [英] Spy on a service method call using jasmine Spies
问题描述
我有以下控制器的 ViewMeetingCtrl.js
(函数(){
使用严格的;
angular.module('MyApp的')控制器('ViewMeetingCtrl',ViewMeetingCtrl)。 。ViewMeetingCtrl $注='$范围,$状态,$ HTTP,$翻译','notificationService','meetingService','$模式,会议,attachmentService']; 功能ViewMeetingCtrl($范围,$状态,$ HTTP,$翻译,notificationService,meetingService,$莫代尔,会议attachmentService){
$ scope.meeting =会议; $ scope.cancelMeeting = cancelMeeting; 功能cancelMeeting(meetingId,companyId){
meetingService.sendCancelNotices(companyId,meetingId)
.success(函数(){
$ state.go('company.view');
});
}
}
})();
我能够succussfully调用spyOn对于 cancelMeeting()而与 sendCancelNotices 方法的调用。我想要做的是,我想测试,只要 cancelMeeting()被调用时,它调用的 sendCancelNotices()方式。我知道我应该createSpy方法去做到这一点。但我不知道该怎么做。
下面是测试案例ViewMeetingCtrlSpec.js
描述('ViewMeetingCtrl CreateSpy - >刺探 - > cancelMeeting',函数(){
变量$ rootScope,范围,$控制器,$ Q;
变种sendCancelNoticesSpy = jasmine.createSpy('sendCancelNoticesSpy');
beforeEach(angular.mock.module('的MyApp')); beforeEach(注(函数($ rootScope,$控制器){
范围= $ rootScope $新的()。
createController =功能(){
返回$控制器('ViewMeetingCtrl',{
$适用范围:适用范围,
会议:{}
});
};
VAR控制器=新createController();
})); 它(跟踪该cancelMeeting间谍被称为功能(){
//一些断言
});});
描述('ViewMeetingCtrl',函数(){ VAR范围,meetingService; beforeEach(angular.mock.module('的MyApp')); beforeEach(注(函数($ rootScope,$控制器,_meetingService_){
范围= $ rootScope $新的()。
meetingService = _meetingService_;
$控制器('ViewMeetingCtrl',{
$适用范围:适用范围,
会议:{}
});
})); 它('应该发送取消通知焕cancelMeeting被称为',函数(){
VAR fakeHttpPromise = {
成功:函数(){}
};
spyOn(meetingService,'sendCancelNotices')andReturn(fakeHttpPromise)。 scope.cancelMeeting('富','棒'); 期待(meetingService.sendCancelNotices).toHaveBeenCalledWith('巴','富');
});});
我会鼓励你停止HTTP承诺依靠从服务返回。相反,只考虑服务返回的承诺。这些都是易于模拟,并不会强迫你重写你的控制器code,当你不返回HTTP的诺言了。
在你的控制器:
函数cancelMeeting(meetingId,companyId){
meetingService.sendCancelNotices(companyId,meetingId)
。然后(函数(){
$ state.go('company.view');
});
}
在您的测试:
VAR fakePromise = $ q.when();
spyOn(meetingService,'sendCancelNotices')and.returnValue(fakePromise); scope.cancelMeeting('富','棒');
期待(meetingService.sendCancelNotices).toHaveBeenCalledWith('巴','富');
I have the following controller ViewMeetingCtrl.js
(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');
});
}
}
})();
I was able to succussfully invoke the spyOn for cancelMeeting() but not with the calling of sendCancelNotices method. What i want to do is , i want to test that whenever cancelMeeting() gets called , it calls sendCancelNotices() method . I know that i should go with createSpy method to do this . But i am not sure how to do it .
Below is the test case 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
});
});
describe('ViewMeetingCtrl', function () {
var scope, meetingService;
beforeEach(angular.mock.module('MyApp'));
beforeEach(inject(function ($rootScope, $controller, _meetingService_) {
scope = $rootScope.$new();
meetingService = _meetingService_;
$controller('ViewMeetingCtrl', {
$scope: scope,
meeting : {}
});
}));
it('should send cancel notices whan cancelMeeting is called', function() {
var fakeHttpPromise = {
success: function() {}
};
spyOn(meetingService, 'sendCancelNotices').andReturn(fakeHttpPromise);
scope.cancelMeeting('foo', 'bar');
expect(meetingService.sendCancelNotices).toHaveBeenCalledWith('bar', 'foo');
});
});
I would encourage you to stop relying of HTTP promises being returned from services. Instead, just consider the service returns a promise. Those are easier to mock, and won't force you to rewrite your controller code when you don't return HTTP promises anymore.
In your controller:
function cancelMeeting(meetingId, companyId) {
meetingService.sendCancelNotices(companyId, meetingId)
.then(function () {
$state.go('company.view');
});
}
In your test:
var fakePromise = $q.when();
spyOn(meetingService, 'sendCancelNotices')and.returnValue(fakePromise);
scope.cancelMeeting('foo', 'bar');
expect(meetingService.sendCancelNotices).toHaveBeenCalledWith('bar', 'foo');
这篇关于间谍使用茉莉间谍服务的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!