需要另一双眼睛来调试 angular 单元测试

Need another set of eyes to debug angular unit tests

我在单元测试中收到了一系列错误消息,并且已经有几个小时没有取得任何进展。如果有人有时间看一下,将不胜感激。这是一个更大的控制器的一部分,所以如果有人想查看更多代码,请告诉我,但我认为这应该涵盖它。

我得到的错误是:

1)

Test 'CampaginLinksController getCampaignLinks():is loading' failed
        Expected undefined to be true.

2)

Test 'CampaginLinksController getCampaignLinks():has a webSiteId that is not 0 and a buyRequestId that is not "" ' failed
    Expected spy mockCampaginLinkSrv.getWebSiteBuyRequestLinks to have been called with [ 54, 8, 432, 200, { nextRowKey : 'fdsf2', nextPartitionKey : '5432gee' } ] but it was never called.

CampaginLinksCtrlSpec.js

describe('CampaignLinksController', function () {

//make module avalible to tests
beforeEach(module('pb.campaignLinks.controllers'));
beforeEach(module('ui.router'));
beforeEach(module('ui.bootstrap'));

var $controller;
var mockPromiseObj;
var length = 200;
var continuationToken = {
    nextRowKey: 'fdsf2',
    nextPartitionKey: '5432gee'
};

var mockCampaignLinkService = {
    //all but delete must return a promiseObj
    getCampaignLinks: jasmine.createSpy('mockCampaignLinkService.getCampaignLinks').and.returnValue(mockPromiseObj),
    getCampaignBuyRequestLinks: jasmine.createSpy('mockCampaignLinkService.getCampaignBuyRequestLinks').and.returnValue(mockPromiseObj),
    getWebSiteBuyRequestLinks: jasmine.createSpy('mockCampaignLinkService.getWebSiteBuyRequestLinks').and.returnValue(mockPromiseObj),
    deleteCampaignLink: jasmine.createSpy('mockCampaignLinkService.deleteCampaignLinks').and.returnValue(mockPromiseObj)
};



var mockGlobal = {
    activeOrganizationId: 54
};

var mockCampaignLinks = true;

var mockCurrentView;

beforeEach(inject(function (_$controller_) {
    $controller = _$controller_;
}));

beforeEach(inject(function ($rootScope, $q) {
    scope = $rootScope.$new();
    var mockPromiseObj = {
        hello: "world",
        then: function (orgId, entityId) {
            var defer = $q.defer();
            defer.resolve(this.account);
            return defer.promise;
        }
    }
    controller = $controller('CampaignLinksController',
        {
            $scope: scope,
            //$stateParams: mockStateParams,
            global: mockGlobal,
            campaignLinks: mockCampaignLinks,
            campaignLinkService: mockCampaignLinkService,
            currentVeiw: mockCurrentView,
            promiseObj: mockPromiseObj 
        });
}));

describe('getCampaignLinks()', function () {

    beforeEach(function () {
        mockCurrentView = {
            campaignId: 32,
            webSiteId: 8
        };
    });

    //describing loading
    it('is loading', function () {
        scope.getCampaignLinks(mockCurrentView, length, continuationToken);
        expect(mockCampaignLinks.loading).toBe(true);
    });

    it('is not loading', function () {
        mockCampaignLinks = false;
        scope.getCampaignLinks(mockCurrentView, length, continuationToken);
        expect(mockCampaignLinks.loading).toEqual(undefined);
    });

    it('has a webSiteId that is not 0 and a buyRequestId that is not "" ', function () {
        mockCurrentView.buyRequestId = 432;
        scope.getCampaignLinks(mockCurrentView, length, continuationToken);
        expect(mockCampaignLinkService.getWebSiteBuyRequestLinks).toHaveBeenCalledWith(mockGlobal.activeOrganizationId, mockCurrentView.webSiteId, mockCurrentView.buyRequestId, length, continuationToken);

        // must check that what is returned is a promise
        expect(scope.promiseObj).toEqual(mockPromiseObj);
    });


});

CampaginLinksController.js

$scope.getCampaignLinks = function (currentView, length, continuationToken) {
            // When loading list items, display loading image
            if ($scope.campaignLinks) $scope.campaignLinks.loading = true;
            var promiseObj = null;
            if (currentView.campaignId && currentView.campaignId !== 0 && !currentView.buyRequestId) {
                promiseObj = campaignLinkService.getCampaignLinks(global.activeOrganizationId, currentView.campaignId, length, continuationToken)
            } else if (currentView.campaignId && currentView.buyRequestId && currentView.campaignId !== 0 && currentView.buyRequestId !== '') {
                promiseObj = campaignLinkService.getCampaignBuyRequestLinks(global.activeOrganizationId, currentView.campaignId, currentView.buyRequestId, length, continuationToken);
            } else if (currentView.webSiteId && currentView.buyRequestId && currentView.webSiteId !== 0 && currentView.buyRequestId !== '') {
                promiseObj = campaignLinkService.getWebSiteBuyRequestLinks(global.activeOrganizationId, currentView.webSiteId, currentView.buyRequestId, length, continuationToken);
            } 

            if (promiseObj) {
                promiseObj.then(function (data) {
                    // If there are already some campaign links being displayed, add newly loaded list to the end
                    if ($scope.campaignLinks) {
                        $scope.campaignLinks.continuationToken = data.continuationToken;
                        $scope.campaignLinks.total += data.total;
                        $.each(data.items, function (index) {
                            $scope.campaignLinks.items.push(data.items[index]);
                        });
                    } else {
                        // Otherwise add loaded list to scope
                        $scope.campaignLinks = data;
                    }
                    // When done loading, hide loading image
                    $scope.campaignLinks.loading = false;
                });
            }
        };

第一次测试:当你初始化mockCampaignLinks时,你应该给它一个对象,而不是true。另外,你可能应该在 beforeEach 中初始化它,因为你在测试中改变了它。

第二个测试:getCampaignBuyRequestLinks 是被调用的方法。