2013-07-25 3 views
0

중첩 된 상태에 대한 단위 테스트를 작성하려고합니다. 상태는 부모 상태에서 일부 범위 요소를 상속받습니다. 내 테스트에서는 분명히 상위 상태를 볼 수 없으므로 상속 된 범위 항목을 볼 수 없습니다.각도 UI 라우터 테스트 범위 상속

it('should show the account page', inject(['$controller', 'security', function ($controller, security){ 
    // set up controller with a scope 
    var $scope = $rootScope.$new(); 

    // the parent controller calls this 
    var user = { first: 'First', last: 'Last', email: '[email protected]', gender:'male', hasPassword:true}; 
    $httpBackend.when('GET', '/api/user/current-user').respond(200, user); 
    $controller('AccountViewCtrl', { $scope: $scope}); 

    $rootScope.$digest(); 

    // verify the initial setup 
    //scope attributes defined on the parent controller 
    expect($scope.user).toEqual(user); 
    expect($scope.account).toEqual(user); 

    // scope attribute defined on the controller 
    expect($scope.genders.length).toEqual(2); 
    expect($scope.editable).toBe(false); 
    expect($scope.editCheck()).toBe(false); 
}])); 

AccountCtrl은 모든 하위 상태에서 필요한 요소 수를 정의합니다.

.controller('AccountCtrl', ['$scope', 'security', function ($scope, security){ 
    $scope.user = security.requestCurrentUser(); 
    $scope.account = angular.copy($scope.user); 
}]) 

는 AccountViewCtrl는 다른 원소를 정의하지만 단지 제어기를 인스턴스화하고 부모 상태의 가시성이 없을뿐만

.controller('AccountViewCtrl', ['$scope', function ($scope) { 
    $scope.editable = false; 
    $scope.genders = [ 
    { name: 'Male', value: 'male' }, 
    { name: 'Female', value: 'female' } 
    ]; 

    $scope.editCheck = function(){ 
    return $scope.editable; 
    }; 

    .... 
}]) 

테스트에 실패한 상위 AccountCtrl의 요소를 상속 관련 요소. ui-router 상태를 테스트하는 최상의 방법이 있습니까?

답변

0

범위 상속은 각도의 기능입니다. 그러므로 나는 내 시험에서 시험 할 필요가 없다고 생각합니다.

it("should set scope when account Ctrl loaded", inject(['$controller', 'security', function($controller, security){ 
    // Add your behavior testing code here 
    var $scope = $rootScope.$new(); 
    var user = { first: 'First', last: 'Last', email: '[email protected]', gender:'male', birthday:'password'}; 
    security.currentUser = user; 
    $controller('AccountCtrl', { $scope: $scope}); 
    $rootScope.$digest(); 

    // verify the initial setup 
    expect($scope.user).toEqual(user); 
    expect($scope.account).toEqual(user); 
}])); 

을 그런 다음 다른 테스트에서 나는 수동으로 값을 채 웁니다 :

it('should set scope on account page', inject(['$controller', 'I18N.MESSAGES', function ($controller, i18nMessages){ 
    // set up controller with a scope 
    var $scope = $rootScope.$new(); 
    var user = { first: 'First', last: 'Last', email: '[email protected]', gender:'male', birthday:'password'}; 
    $scope.account = $scope.user = user; 
    ... 
}])); 

내 해결 방법은 AccountCtrl 작동 후 바로 다른 시험의 값을 넣어 테스트하는 것입니다

관련 문제