2014-02-13 2 views
5

방금 ​​ng-table을 사용하기 시작했으며 재스민 단위 테스트를 통해 테스트하려고합니다. (이것이 내 컨트롤러를 테스트하는 가장 좋은 방법인지 확실하지 않습니다.) 그리고 나는이 오류가 계속 발생한다. I 설치 ngTableParams 내 컨트롤러

여기
# Setup ng-table with defaults and data 
    $scope.tableParams = new ngTableParams({ 
    page: 1, 
    count: 10, 
    sorting: { code: 'asc'} 
    }, { 
    # data is created by ngTableParams 
    total: 0, 

    # ng-table will ask for updated data, this is where it gets it from. 
    getData: ($defer, params) -> 
     # Go get a list of debtors 
     Debtor.query {}, (data) -> 
     # Once successfully returned from the server with my data process it. 
     params.total(data.length) 

     # Filter 
     filteredData = (if params.filter then $filter('filter')(data, params.filter()) else data) 

     # Sort 
     orderedData = (if params.sorting then $filter('orderBy')(filteredData, params.orderBy()) else data) 

     # Paginate 
     $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count())) 
    }) 

는 자스민 테스트 여기

# ================================================================================ 
    # Delete tests 
    # ================================================================================ 
    describe "destroy", -> 
    beforeEach(inject (($controller, $rootScope, $location, $state, $httpBackend) -> 
     ctrl = $controller('DebtorsController', { 
     $scope: @scope, 
     $location: $location 
     }) 
    )) 

    it "gets new listing", -> 
     @scope.debtors = [{id: 1, name: "Good guys"}, {id: 2, name: "John"}] 
     @httpBackend.whenDELETE('/clients/1').respond(200) 
     @httpBackend.whenGET('/debtors').respond(200, [{name: "John"}]) 
     @scope.destroy(1) 
     @httpBackend.flush() 
     expect(@scope.debtors[0].name).toEqual('John') 

(작동) 삭제하는 내 코드

# -------------------------------------------------------------------------------- 
    # Delete 
    # -------------------------------------------------------------------------------- 
    $scope.destroy = (id) -> 
    Client.delete 
     id: id 
    , (response) ->    # Success 
     flash("notice", "Client deleted", 2000) 
     $scope.tableParams.reload() # Reload ng-table data, which will get a new listing from the server. 
    , (response) ->    # Failure 
가 어디 여기

TypeError: Cannot set property '$data' of null 
    at project/vendor/assets/javascripts/ng-table-0.3.1/ng-table.src.js:396:55 
    at C (project/vendor/assets/javascripts/angular-min.js:92:375) 
    at project/vendor/assets/javascripts/angular-min.js:94:28 
    at h.$eval (project/vendor/assets/javascripts/angular-min.js:102:308) 
    at h.$digest (project/vendor/assets/javascripts/angular-min.js:100:50) 
    at h.$apply (project/vendor/assets/javascripts/angular-min.js:103:100) 
    at f (project/vendor/assets/javascripts/angular-min.js:67:98) 
    at handleResponse (project/vendor/assets/javascripts/angular-mocks.js:1160:9) 
    at Function.$httpBackend.flush (project/vendor/assets/javascripts/angular-mocks.js:1491:26) 
    at null.<anonymous> (project/spec/javascripts/unit/debtors_controller_spec.js.js:50:24) 

입니다

답변

6

코드를 표시 할 수 있습니까? ngTableParams를 초기화하는 컨트롤러에서?

문제는 ng-table은 을 사용하여 을 $scope으로 설정합니다. 당신은 단위 테스트이기 때문에 ngTableController을 갖지 않을 것입니다. 은 ngTableParams이 될 것입니다. 단위 테스트에서 수동으로 $scopengTableParams으로 설정해야합니다. JS이 비슷한 시도 (정말 커피 스크립트를 싫어하는을, 그래서 당신의 요구로 변환) :

beforeEach(inject(function ($controller, $rootScope) { 
      scope = $rootScope.$new(); 
      ctrl = $controller('DebtorsController', { 
       $scope: scope, 
       $location: $location 
      }); 
      scope.tableParams.settings().$scope = scope; 
     })); 

을 당신이 컨트롤러에 다음과 같은 뭔가를 가정 당신이 당신의 ngTableParams를 초기화 여기서

$scope.tableParams = new ngTableParams({ 
    page: 1,   // show first page 
    count: 10   // count per page 
}, { 
    total: data.length, // length of data 
    getData: function($defer, params) { 
     $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count())); 
    } 
}); 
+0

을 Ctrl 키를 사용. $ scope = scope가 작동하지 않는 것 같습니다. 정의되지 않은 메서드 'settings'를 호출 할 수 없습니다. – map7

+0

@ map7 첫 번째 줄에 설명 된대로 (ngTableParams.settings(). $ scope = scope 또는 ctrl.tableParams.settings 내 답변 : "ngTableParams를 초기화하는 컨트롤러의 코드를 표시 할 수 있습니까?" 컨트롤러에서 노출해야하는 ngTableParams 변수를 참조해야합니다. 예를 들어, 컨트롤러 내에'$ scope.tableParams = new ngTableParams = new nTableParams (......)'가 있다면 단위 테스트는'scope.tableParams.settings(). $ scope = scope;'가 있어야합니다. 대답. – Beyers

+0

내 질문을 내 컨트롤러로 업데이트했습니다. 테스트가 조금 더 진행 중입니다. 이제 기대 선에 대해 불평하고 있습니다. '존'과 동등하지 않고 '선한 사람'과 '존'으로 설정되어 있습니다. 브라우저를 통해 수동으로 테스트하면 작동합니다. @ scope.debtors [0] .name을 확인해야합니까? 아니면 최종 결과를 위해 다른 것을 조사해야합니까? – map7