模拟不用于单元测试

时间:2015-01-23 20:16:18

标签: angularjs unit-testing jasmine

我很难过。我现在已经编写了几次模态测试但是在我正在测试的控制器中我一直收到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);
        });

    });

});

1 个答案:

答案 0 :(得分:1)

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

'campaign', 'accountId', 'pressReleaseCampaignService'

VS

pressReleaseCampaignService, campaign, accountId

因此,当您的代码在pressReleaseCampaignService上调用方法时,它实际上会在广告系列服务上调用方法。