模拟未在单元测试中使用

Mock not being used in Unit Test

我很难过。我现在已经编写了几次模态测试,但是在我目前正在测试的控制器中,我不断收到 TypeError: 'undefined' is not a function (evaluating 'pressReleaseCampaignService.deleteCampaign(global.activeOrganizationId, accountId, campaign.entityId) 错误。我希望测试使用我的模拟,但我不确定为什么它一直引用原始控制器。有人看到我的错误吗?

控制器:

angular.module('pb.campaigns.controllers')
    .controller('ConfirmDeleteModalController', ['$scope', '$stateParams', '$state', '$modal', '$modalInstance', 'global', 'pbRoles', 'campaign', 'accountId', 'pressReleaseCampaignService',
        function ($scope, $stateParams, $state, $modal, $modalInstance, global, pbRoles, pressReleaseCampaignService, campaign, accountId) {

            $scope.campaign = campaign;
            $scope.currentAccountId = accountId;

            $scope.delete = function (accountId, campaign) {
                global.setFormSubmitInProgress(true);
                pressReleaseCampaignService.deleteCampaign(global.activeOrganizationId, accountId, campaign.entityId).then(function () {
                    global.setFormSubmitInProgress(false);
                    $modalInstance.close();
                },
                function (errorData) {
                    global.setFormSubmitInProgress(false);
                });

            };

            $scope.cancel = function () {
                global.setFormSubmitInProgress(false);
                $modalInstance.dismiss('cancel');
            };

        }]);

测试:

describe('ConfirmDeleteModalController', function () {
    beforeEach(module('pb.roles'));
    beforeEach(module('pb.campaigns.controllers'));
    beforeEach(module('ui.router'));
    beforeEach(module('ui.bootstrap'));

    var mockModalInstance = {};
    var mockcampaignBuyRequestService = {};
    var mockGlobal = {};
    var mockStateParams = {};
    var mockState = {};
    var mockPbRoles = {};

    beforeEach(inject(function ($q) {

        mockGlobal = {
            setFormSubmitInProgress: function (boolean) {
                this.formProgress = boolean;
            },
            formProgress: false,
            activeOrganizationId: 0
        };
        mockStateParams = {
            orgId: 1,
            accountId: 3,
            entityId: 94,
            campaignId: 45,
            page: 3,
            length: 12
        };
        mockModalInstance = {
            close: jasmine.createSpy('mockModalInstance.close'),
            dismiss: jasmine.createSpy('mockModalInstance.dismiss'),
            result: {
                then: jasmine.createSpy('mockModalInstance.result.then')
            }
        };
        mockPressReleaseCampaignService = {
            campaign: { entityId: 2, page: 19, length: 200 },
            deleteCampaign: function (activeOrganizationId, entityId) {
                var defer = $q.defer();
                defer.resolve();
                return defer.promise;
            }
        };

    }));

    beforeEach(inject(function ($rootScope, _$controller_) {
        scope = $rootScope.$new();
        $controller = _$controller_;
        controller = $controller('ConfirmDeleteModalController', {
            $modalInstance: mockModalInstance,
            $scope: scope,
            $stateParams: mockStateParams,
            $state: mockState,
            global: mockGlobal,
            pbRoles: mockPbRoles,
            pressReleaseCampaignService: mockPressReleaseCampaignService,
            campaign: function () {
                return mockPressReleaseCampaignService.campaign
            },
            accountId: function () {
                return mockPressReleaseCampaignService.accountId
            }
        });

    }));


    describe("delete() function", function () {

        it("calls deleteCampaign with proper params", function () {
            spyOn(mockPressReleaseCampaignService, "deleteCampaign").and.callThrough();
            scope.delete(mockStateParams.accountId, mockPressReleaseCampaignService.campaign);
            expect(mockPressReleaseCampaignService.deleteCampaign).toHaveBeenCalledWith(mockGlobal.activeOrganizationId, mockStateParams.accountId, mockPressReleaseCampaignService.campaign.entityId);
        });


        it("calls close and toggles setFormSubmitionInProgress status", function () {
            scope.delete(mockStateParams.accountId, mockPressReleaseCampaignService.campaign);
            expect(mockGlobal.formProgress).toEqual(true);
            scope.$digest();
            expect(mockModalInstance.close).toHaveBeenCalled();
            expect(mockGlobal.formProgress).toEqual(false);
        });

    });

    describe("cancel() function", function () {

        it("changes formProgress from true to false", function () {
            mockGlobal.setFormSubmitInProgress.formProgress = true;
            scope.cancel();
            expect(mockModalInstance.dismiss).toHaveBeenCalled();
            expect(mockGlobal.formProgress).toEqual(false);
        });

    });

});

服务名称的顺序与变量的顺序不同:

'campaign', 'accountId', 'pressReleaseCampaignService'

对比

pressReleaseCampaignService, campaign, accountId

因此,当您的代码调用 pressReleaseCampaignService 上的方法时,它实际上调用了活动服务上的方法。