2013-07-30 1 views
0

테스트하기 전에 지시문을 설정해야하는 지시문에 대한 단원 테스트에 문제가 있습니다. BeforeEach with expectGets는 한 번 이상 실행될 수 없습니다.

내가 그것을 초기화 방법입니다 :

var elm, $templateCache 
var count = 0; 

beforeEach(module('app/js/directives/templates/mytemplate.html')); 


beforeEach(inject(function($injector) { 
    $templateCache = $injector.get('$templateCache'); 
})); 

beforeEach(inject(
     ['$httpBackend','$compile','$rootScope', function(_$compile) { 
      var template = $templateCache.get('app/js/directives/templates/mytemplate.html'); 
      if(count == 0){ 
       $httpBackend.expectGET('js/directives/templates/mytemplate.html').respond(template); 
      } 

      elm = angular.element('<div my-directive</div>'); 
      $compile(elm)($rootScope); 


      if(count == 0){ 


       $httpBackend.expectGET('/app/js/content/some.json').respond('{ "left" : "<div>Contact information</div>"}'); 

       $httpBackend.flush(); 
      } 

      $rootScope.$digest(); 
      count++; 
     }] 
    )); 

afterEach(function() { 
    $httpBackend.verifyNoOutstandingExpectation(); 
    $httpBackend.verifyNoOutstandingRequest(); 
}); 


it("should work like I want it to", function() { 
    $rootScope.my-directive.somefunction(); 
    expect($rootScope.somevalue).toBe(2); 
}); 

it("should load the previous page if the function scope.layover.qa_pager.prev is called", function() { 
    $rootScope.my-directive.someotherfunction(); 
    expect($rootScope.somevalue).toBe(1); 
}); 

이 작동하지만 카운트 해킹은 내 의견으로는 필요하지 않을 것이다. 만약 내가 떠나면 $ httpbackend에 문제가 생기므로 매번 다시 초기화되지 않는 것 같아서 '오류 : 불만족스러운 요청 : GET /app/js/content/some.json'및 '오류 : 보류 중이 아닙니다. 플러시 요청! '

답변

0

내게는 단위 테스트 용 템플릿을로드하려고하는 것 같습니다. 나는 테스트를 실행하기 위해 카르마를 사용하고 있다고 가정하고 있으므로 html2js를 살펴 봐야합니다. 템플릿을 템플릿 캐시에로드합니다. 나는 그것을 시도했다

// karma.conf.js 
module.exports = function(config) { 
    config.set({ 
    preprocessors: { 
     '**/*.html': ['ng-html2js'] 
    }, 

    files: [ 
     '*.js', 
     '*.html' 
    ], 

    ngHtml2JsPreprocessor: { 
     // strip this from the file path 
     stripPrefix: 'public/', 
     // prepend this to the 
     prependPrefix: 'served/', 

     // or define a custom transform function 
     cacheIdFromPath: function(filepath) { 
     return cacheId; 
     } 
    } 
    }); 
}; 
+0

https://github.com/karma-runner/karma-ng-html2js-preprocessor

하지만, NG-html2js 우리 팀은 아직 사용할 수없는 새로운 카르마입니다, 나는 맹 글링 경로를해야합니까 .. – Maarten

관련 문제