需要另一组眼睛来调试角度单位测试

时间:2015-01-15 03:19:54

标签: angularjs unit-testing jasmine

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

我得到的错误是:

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

1 个答案:

答案 0 :(得分:0)

第一个测试:初始化mockCampaignLinks时,应该给它一个对象,而不是true。此外,您可能应该在beforeEach中初始化它,因为您在测试中将其变异。

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