间谍使用茉莉间谍服务的方法调用

编程入门 行业动态 更新时间:2024-10-21 14:25:38
本文介绍了间谍使用茉莉间谍服务的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下控制器的 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');

更多推荐

间谍使用茉莉间谍服务的方法调用

本文发布于:2023-10-31 06:08:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间谍   茉莉   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!