2015-01-15 61 views
0

我在單元測試中得到一系列錯誤消息,並且自從我取得了一些進展以來,它已經過了幾個小時。如果任何人有時間看看它將不勝感激。這是一個更大的控制器的一部分,所以如果任何人想看到更多的代碼,請讓我知道,但我認爲這應該覆蓋它。需要另一組眼睛來調試角度單元測試

我得到的錯誤是:

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

你能告訴我爲什麼你有條件'if($ scope.campaignLinks)....;'你能不能簡單地初始化'$ scope.campaignLinks.loading = false' ?,謝謝 –

+0

其實它不是我的代碼基礎。我負責在重新編碼代碼之前編寫測試。我會記住這一點,並感謝您的期待! – ReganPerkins

回答

0

第一個測試:初始化mockCampaignLinks時,應該給它一個對象,而不是true。另外,你可能應該在beforeEach中初始化它,因爲你在測試中改變了它。

第二次測試:getCampaignBuyRequestLinks是被調用的方法。

+0

仍然有一些mockCampaignLinks麻煩,但你的幫助是巨大的!謝謝 – ReganPerkins