angularjs – 间谍服务方法调用使用茉莉花间谍

我有以下控件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方法去做.但我不知道该怎么做

下面是测试用例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');

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...