使用 jasmine Spies 监视服务方法调用 [英] Spy on a service method call using jasmine Spies

查看:22
本文介绍了使用 jasmine Spies 监视服务方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我能够成功地为 cancelMeeting() 调用 spyOn,但不能通过调用 sendCancelNotices 方法.我想要做的是,我想测试每当 cancelMeeting() 被调用时,它都会调用 sendCancelNotices() 方法.我知道我应该使用 createSpy 方法来做到这一点.但我不知道该怎么做.

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 .

下面是测试用例ViewMeetingCtrlSpec.js

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');
    });

});

我鼓励您停止依赖从服务返回的 HTTP 承诺.相反,只需考虑服务返回一个承诺.这些更容易模拟,并且不会在您不再返回 HTTP 承诺时强迫您重写控制器代码.

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.

在您的控制器中:

    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');

这篇关于使用 jasmine Spies 监视服务方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆