2015-01-09 70 views
1

您好,我需要幫助編寫此控制器功能的測試。我認爲我很接近,但我一直在期待undefined等於{entityId:2,page:19,length:200}。這似乎很奇怪,因爲我有另一個幾乎相同的測試。如果任何人可以發現錯誤或有任何一般性的測試建議,我會非常感激。單元測試控制器 - 需要第二雙眼睛

謝謝!

$scope.getAll = function (page, length) { 

     accountService.getAccounts(global.activeOrganizationId, page, length).then(function (data) { 
      $scope.accounts = data; 
     }); 

    }; 

這是我迄今爲止

describe('AccountsController', function() { 
    //make module avalible to tests 
    beforeEach(module('pb.accounts.controllers')); 
    beforeEach(module('ui.router')); 
    beforeEach(module('ui.bootstrap')); 

    var $controller; 
    var mockGlobal = { activeOrganizationId: 0 }; 
    var mockStateParams = { orgId: 1, entityId: null }; 
    var mockForm = {}; 

    var mockAccountSrv = { 
     account: { 
      entityId: 2, 
      page: 19, 
      length: 200 
     } 
    }; 

    // instantiating controller 
    beforeEach(inject(function (_$controller_) { 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $controller = _$controller_; 
    })); 

describe("getAll() function", function() { 
     var controller, scope; 


     beforeEach(inject(function ($q) { 
      mockAccountSrv.getAccounts = function (entityId, page, length) { 
       var defer = $q.defer(); 
       defer.resolve(this.account); 
       return defer.promise; 
      }; 
     })); 

     // sets scope of controller before each test 
     beforeEach(inject(function ($rootScope) { 
      scope = $rootScope.$new(); 
      controller = $controller('AccountsController', 
       { 
        $scope: scope, 
        $stateParams: mockStateParams, 
        global: mockGlobal, 
        accountService: mockAccountSrv 
       }); 
     })); 

     it("make sure service promise resolves", function() { 
      scope.getAll(mockAccountSrv.account.page, mockAccountSrv.account.length); 
      scope.$digest(); 

      expect(scope.account).toEqual(mockAccountSrv.account); 
     }); 

    }); 
+1

難道是你正在測試'scope.account'而數據到'$ scope.accounts'(最後的「s」)? – 2015-01-09 20:11:26

回答

0

如果你想知道我用mockAccountSrv.account代替.accounts 感謝,雖然找了一個愚蠢的錯字!並感謝Nikos