7

Angular-UI Bootstrap 구성 요소의 속성 인 resolve 속성으로 올바른 변수를 보내고 있다고 주장하는 단위 테스트를 작성하려고합니다. . 여기에 지금까지 무엇을 가지고 :각도 UI 부트 스트랩 모달 구성 요소에서 'resolve'속성을 단위 테스트하는 방법

// Controller 
angular.module('app') 
    .controller('WorkflowListCtrl', function ($scope, $modal) { 
    // Setup the edit callback to open a modal 
    $scope.edit = function(name) { 
     var modalInstance = $modal.open({ 
     templateUrl: 'partials/editWorkflowModal.html', 
     controller: 'WorkflowEditCtrl', 
     scope: $scope, 
     resolve: { 
      name: function() { return name; } 
     } 
     }); 
    }; 
    }); 

그것은 resolve.name 속성은 각도-UI 구성 요소가 제대로 작동 할 수있는 기능이 있어야한다는 지적 가치 - 이전에 나는 resolve: { name: name }을 시도했다하지만이 작동하지 않았다. 순간

// Unit Test 
describe('Controller: WorkflowListCtrl', function() { 

    // load the controller's module 
    beforeEach(module('app')); 

    var workflowListCtrl, 
    scope, 
    modal; 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, $rootScope) { 

    scope = $rootScope.$new(); 
    modal = { 
     open: jasmine.createSpy() 
    }; 

    workflowListCtrl = $controller('WorkflowListCtrl', { 
     $scope: scope, 
     $modal: modal 
    }); 

    it('should allow a workflow to be edited', function() { 
     // Edit workflow happens in a modal. 
     scope.edit('Barney Rubble'); 
     expect(modal.open).toHaveBeenCalledWith({ 
     templateUrl: 'partials/editWorkflowModal.html', 
     controller: 'WorkflowEditCtrl', 
     scope: scope, 
     resolve: { 
      name: jasmine.any(Function) 
     } 
     }); 
    }); 
    })); 
}); 

이 단지 resolve.name 속성은 함수임을 확인하지만, 내가 정말하고 싶은 것은 resolve.name 기능이 Barney Rubble을 반환 주장이다. 이 구문은 분명히 작동하지 않습니다 어떻게 든이 Barney Rubble 호출했지만, 난 그렇게 할 수있는 방법을 알아낼 수 없습니다 확인하기 위해 resolve.name 기능을 감시 할 같은

expect(modal.open).toHaveBeenCalledWith({ 
    templateUrl: 'partials/editWorkflowModal.html', 
    controller: 'WorkflowEditCtrl', 
    scope: scope, 
    resolve: { 
    name: function() { return 'Barney Rubble'; } 
    } 
}); 

것 같다. 어떤 아이디어?

+0

어쩌면 당신이 볼 수 : http://stackoverflow.com/questions/26853603/unit-testing-angular-bootstrap-modal/26942188 –

답변

5

그래서이 방법을 알아 냈습니다.

$scope에 '개인'기능을 정의합니다

$scope._resolve = function(item) { 
    return function() { 
    return item; 
    }; 
}; 

이 '개인'메서드를 호출 원래 $scope 함수를 수정 :

$scope.edit = function(name) { 
    var modalInstance = $modal.open({ 
    templateUrl: 'partials/modal.html', 
    controller: 'ModalCtrl', 
    scope: $scope, 
    resolve: { 
     name: $scope._resolve(name) 
    } 
    }); 
}; 

업데이트 당신의 검사 결과가이 기능을 조롱하고을 반환을 원래 값, 그럼 당신은 그것이 정확하게 전달되었는지 테스트 할 수 있습니다.

it('should allow a workflow to be edited', function() { 
    // Mock out the resolve fn and return our item 
    spyOn($scope, '_resolve').and.callFake(function(item) { 
    return item; 
    }); 

    // Edit workflow happens in a modal. 
    scope.edit('Barney Rubble'); 
    expect(modal.open).toHaveBeenCalledWith({ 
    templateUrl: 'partials/modal.html', 
    controller: 'ModalCtrl', 
    scope: scope, 
    resolve: { 
     name: 'Barney Rubble' 
    } 
    }); 
}); 
+1

이 또 다른 해결책이 될 수있다 : http://stackoverflow.com/ 질문/26853603/unit-testing-angular-bootstrap-modal/26942188 –

+1

$ scope._resolve 메서드에 대해 별도의 단위 테스트를 작성하는 방법은 무엇입니까? – EnugulaS

관련 문제