大佬教程收集整理的这篇文章主要介绍了angularjs – 间谍服务方法调用使用茉莉花间谍,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
(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'); }); } } })();
我能够成功地调用了用于cancelMeeTing()的spyOn,但是没有调用sendCancelNotices方法.我想做的是,我想测试当cancelMeeTing()被调用时,它调用sendCancelNotices()方法.我知道我应该用createSpy方法去做.但我不知道该怎么做
下面是测试用例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,_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'); }); });
我鼓励你停止依赖从服务返回的http承诺.相反,只要考虑服务返回承诺.那些更容易嘲笑,并且不再强制您在不再返回http承诺时重写控制器代码.
在你的控制器中:
function cancelMeeTing(meeTingId,CompanyId) { meeTingservice.sendCancelNotices(CompanyId,meeTingId) .then(function () { $state.go('company.view'); }); }
在你的测试中:
var fakePromise = $q.when(); spyOn(meeTingservice,'sendCancelNotices')and.returnValue(fakePromisE); scope.cancelMeeTing('foo','bar'); expect(meeTingservice.sendCancelNotices).toHaveBeenCalledWith('bar','foo');
以上是大佬教程为你收集整理的angularjs – 间谍服务方法调用使用茉莉花间谍全部内容,希望文章能够帮你解决angularjs – 间谍服务方法调用使用茉莉花间谍所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。