는 방법을 테스트하는 방법을 전달되는 지시어 컨트롤러
-
20-12-2019 - |
문제
나는 방법에 정의되어 컨트롤러입니다.이 방법은 통과하는 사용자 정의 어떤 지시어는 격리된 범위가 있습니다.를 테스트하려면 어떻게 해야 합 구현 $scope.search
가 제대로 하고 있습니까?
AutocompleteCtrl
$scope.query = function (term) {
// some async stuff
};
템플릿
<div ng-controller="AutocompleteCtrl">
<div typeahead data-search="query(term)"></div>
</div>
지시어(발췌)
return {
controller: 'AutocompleteDirectiveCtrl',
scope: {
search: '&'
}
}
AutocompleteDirectiveCtrl
$scope.query = function (term) {
if (term.length > ($scope.minChars - 1)) {
$scope.search({term: term});
}
};
단위 테스트
describe('Autocomplete AutocompleteDirectiveCtrl', function () {
var $scope, $window;
beforeEach(module('myApp'));
beforeEach(module(function ($provide) {
$window = {
location: {},
document: window.document
};
// We register our new $window instead of the old
$provide.constant('$window', $window);
}));
beforeEach(inject(function ($rootScope, $controller) {
$scope = $rootScope.$new();
$controller('AutocompleteDirectiveCtrl', {$scope: $scope});
}));
describe('Ducktyping', function () {
it('should contain a submit method', function () {
expect($scope.submit).toBeDefined();
});
});
describe('Controller Functionality', function () {
it('should return false if a submit request has no term set', function () {
expect($scope.submit()).toEqual(false);
});
it('should redirect to search-results page if a term is present', function () {
$scope.action = 'search-results.html';
$scope.submit('mySearchTerm');
expect($window.location.href).toBe('search-results.html/products/mySearchTerm');
});
it('should do a searchquery if term length is equal or longer then min-chars', function () {
$scope.minChars = 3;
$scope.query('myTerm');
});
});
});
로그인
TypeError: 'undefined' is not a function (evaluating '$scope.search({term: term})')
해결책
그것을 밖으로 변했습니다.나는 단지를 조롱하는 데 필요한 방법이다.
it('should do a searchquery if term length is equal or bigger then min-chars', function () {
$scope.minChars = 3;
$scope.search = function (term) {
return term;
};
spyOn($scope, 'search');
$scope.query('bes');
expect($scope.search).toHaveBeenCalledWith({term: 'bes'});
});
제휴하지 않습니다 StackOverflow