1

내가 물었다 비슷한 질문을 본 적이 있지만 그 중 하나는 다음과 가정 문제의 근본 해결 : 나는 ngMockE2E을 사용하지 않는 때문에,templateUrl을 사용하여 지시 단위 테스트에서 AngularJS app config 블록이 실행되는 것을 어떻게 중지합니까?

  1. 내가 사용할 수 없습니다 whenGET('').passThrough()을.
  2. ngMockE2E을 사용해야 할 필요는 없습니다. 왜냐하면 말 그대로 "바"를 내뱉는 지시어에 대한 단위 테스트를 작성하고 있기 때문입니다.
  3. 하나의 제안은 이러한 HTTP 응답을 처리하기 위해 use a proxy server을 제안하는 것이지만 단위 테스트의 목적을 무효화하지는 않습니까?

    describe('Directive: foo', function() { 
    
        // load the directive's module 
        beforeEach(module('app')); 
    
        var $compile; 
        var $rootScope; 
        var $httpBackend; 
    
        beforeEach(inject(function(_$compile_, _$rootScope_, $injector) { 
        $compile = _$compile_; 
        $rootScope = _$rootScope_; 
        $httpBackend = $injector.get('$httpBackend'); 
        })); 
    
        afterEach(function() { 
        $httpBackend.verifyNoOutstandingExpectation(); 
        $httpBackend.verifyNoOutstandingRequest(); 
        }); 
    
        it('should be bar', inject(function($templateCache) { 
        $templateCache.put('templates/bar.html', 'bar'); 
        var element = $compile('<div data-foo></div>')($rootScope); 
    
        //$httpBackend.whenGET(/^\/translations\//).passThrough(); // doesn't work 
        $httpBackend.expectGET(/\/translations\//).respond('201', ''); // works 
        $rootScope.$digest(); 
        $httpBackend.flush(); 
    
        expect(element.text()).toBe('bar'); // works 
        })); 
    }); 
    

    지금, 시험이 모든 가짜 $httpBackend 사업과 함께 잘 작동합니다 : 여기

    angular.module('app') 
        .directive('foo', function() { 
        return { 
         restrict: 'A', 
         templateUrl: 'templates/bar.html' 
        }; 
        }); 
    

    그리고 단위 테스트입니다 : 이제

, 내가 당신에게 내 지시어를 보여주지 거기,하지만 이건 완전히 내 단위 테스트에 속하지 않습니다! 이 $httpBackend을 어떻게 잡아 당겨서 AngularJS app config 블록이 templateUrl을 사용하여 내 지시문 단위 테스트에서 실행되지 않게합니까?

참고 : 지시문에 templateUrl이있는 경우에만 발생합니다.

$translatePartialLoaderProvider.addPart('index'); 
$translateProvider.useLoader('$translatePartialLoader', { 
    urlTemplate: '/translations/{part}/{lang}.json' 
}); 

그러나 나는 반드시 관련이 있다고 생각하지 않습니다

BTW, 응용 프로그램의 config 블록이 HTTP 요청을 트리거 것 코드입니다.

답변

1

지시어를 자신의 모듈에 넣고 기본 모듈에 해당 모듈을 포함 시키십시오. 그런 다음 앱 모듈을로드하지 않고 지시문 모듈을 테스트 할 수 있습니다.

angular.module('app-directives', []) 
    .directive('foo', function() { 
    return { 
     restrict: 'A', 
     templateUrl: 'templates/bar.html' 
    }; 
    }); 

angular.module('app', ['app-directives']) 
    //Run your config stuff here