angularjs
  • karma-jasmine
  • jasmine2.0
  • 2017-01-15 8 views 0 likes 
    0

    を取得することはできません。テスト角度/ジャスミンディレクティブは、私がtemplateUrlが含まれている角度ディレクティブの以下のカルマ/ジャスミンのテストを持っているテンプレートHTML

    describe("topbar Directive", function() { 
    
        var scope,element; 
    
        beforeEach(module('app')); 
        beforeEach(module('app/directives/topbar.html')); 
    
        var title = 'This is the title'; 
    
        beforeEach(inject(function ($rootScope,$compile) { 
         console.log(1) 
         var formElement = angular.element('<div top-bar title="\'' + title + 
           '\'" on-close="modalCancel()"></div>'); 
         console.log(2) 
         scope = $rootScope.$new(); 
         console.log(3) 
         element = $compile(formElement)(scope); 
         console.log(4) 
         scope.$digest(); 
         console.log(5) 
        })); 
    
    
        it("should have a title", function() { 
         console.log(6) 
         expect(element.find('title')).toEqual(title); 
        }) 
    
    }); 
    

    テスト結果がconsole.log(5)を印刷し、例外をスローしません。何が問題なの?

    15 01 2017 12:36:53.239:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/ 
    15 01 2017 12:36:53.242:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency 
    15 01 2017 12:36:53.248:INFO [launcher]: Starting browser PhantomJS 
    15 01 2017 12:36:54.770:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#0Ml05I-NApeqvN5wAAAA with id 22588553 
    LOG: 1 
    LOG: 2 
    LOG: 3 
    LOG: 4 
    LOG: 6 
    PhantomJS 2.1.1 (Windows 8 0.0.0) topbar Directive should have a title FAILED 
         Error: Unexpected request: GET assets/app/directives/topbar.html 
         No more request expected (line 1245) 
         [email protected]/libs/angular/angular-mocks.js:1245:90 
         [email protected]/libs/angular/angular.js:10558:21 
         [email protected]/libs/angular/angular.js:10268:23 
         [email protected]/libs/angular/angular.js:14792:30 
         test/libs/angular/angular.js:14808:39 
         [email protected]/libs/angular/angular.js:16052:28 
         [email protected]/libs/angular/angular.js:15870:36 
         test/directives/topbar.test.js:19:16 
         [email protected]/libs/angular/angular.js:4523:22 
         [email protected]/libs/angular/angular-mocks.js:2439:26 
         [email protected]://localhost:9876/context.js:151:17 
         undefined 
         Expected ({ length: 0, prevObject: ({ 0: HTMLNode, length: 1 }), context: undefined, selector: 'title' }) to equal 'This is the title'. 
    

    UPDATE

    私はng-html2jsカルマプラグインkarma.conj.jsで

    でテンプレートをロードしています:

    preprocessors: { 
        'app/directives/*.html': 'ng-html2js' 
    }, 
    ngHtml2JsPreprocessor: { 
        stripPrefix: 'assets/' 
    }, 
    

    そして、これは私がテストしていますディレクティブです、templateUrl note

    angular.module('app'). 
    directive("topBar", function() { 
         return { 
         restrict: "AE",   
         scope: {   
          title: '=' 
         }, 
         templateUrl: "assets/app/directives/topbar.html", 
         link: function (scope, element, attrs) { 
          scope.modalClose = function() { 
           // ... some code 
          } 
         } 
         } 
    }); 
    

    答えて

    0

    角度単位テストでは実際の要求はできません。

    すべてtemplateUrlテンプレートは、事前にそれらを要求したアクション(この場合は$compileコール)に

    $templateCache.put('assets/app/directives/topbar.html', ...topbar.html contents...)

    と嘲笑されなければなりません。

    +0

    私はすでにキャッシュにテンプレートをロードしている 'ng-html2js'を使用しています。 – ps0604

    +0

    あなたは質問から除外しましたが、ここで最も関連性の高い部分です。 ** app/directives/topbar.html **ではなく** assets/app/directives/topbar.html **なので、これは問題だと思います。 – estus

    +0

    'console.log($ templateCache.get( 'app/directives/topbar.html'));'を追加すると、テンプレートがロードされているのがわかります。 。 – ps0604

    関連する問題