AngularJS 유닛 테스트를 처음 사용했습니다. 재스민 컨트롤러로 함수를 테스트하고 싶습니다. 동일한 컨트롤러에서 또한 내가하지이를 테스트 할 할 $ locationChangeStart
$scope.$on('$locationChangeStart', function(){
$scope.killTimer($scope.timer);
});
위한 시계,하지만 또 다른 기능이 있습니다. 내가 테스트를 실행하려고하면 아직도, 나는 $ 범위를 가리키는 오류를 얻을 라인에 $ :.
TypeError: undefined is not a function
나는 시험이 줄을 실행할 수 있도록하려면 어떻게해야합니까? 나는 모든 것이 정상인지 확신 할 수 없기 때문에 테스트 스펙을 복사한다.
describe('invoiceListController', function() {
beforeEach(module('invoice'));
var $controller;
beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
var $scope, controller;
beforeEach(function() {
$scope = {};
controller = $controller('invoiceList', { $scope: $scope });
});
describe('deleteInvoices()', function(){
it('should do nothing when no invoice is selected', function() {
$scope.invoicesToDelete = false;
var response = $scope.deleteInvoices();
expect(response).toEqual(false);
});
});
});
그리고 코드는 내가 테스트 할 : Plunker에서
$scope.deleteInvoices = function(){
if($scope.invoicesToDelete) {
return true;
} else {
return false;
}
};
예 : $를 필요로 Plunker
$ scope.killTimer는 사용할 수 있어야하지만, 처음부터 호출하면 안되기 때문에 중요하지 않습니다. 하지만 $ scope()을 테스트 코드에 포함하려고 시도했는데 undefined라고 알려줍니다. 나는 시험 준비에 뭔가 빠졌지 만, 무엇을 발견 할 수 없었던 것 같아. (응용 프로그램의 이름은 '인보이스'이고 컨트롤러의 이름은 'invoiceList'입니다.) – Cathfaern
전체 컨트롤러를보고 테스트 할 수 있도록 jsfiddle을 설정하면 모든 것을 볼 필요없이 올바르게 볼 수 있습니다 어려워. 또한 locationChangeStart가 컨트롤러의 초기화로 실행 중일 수 있습니다. –
질문에 큰 예를 추가했습니다. – Cathfaern