3

何が問題なのかわからない...

カルマ.conf.js

  files: [
  // TEMPLATES
  'app/scripts/directives/views/profile_picture.html',

  preprocessors : {
      // generate js files from html templates
      'app/scripts/directives/views/profile_picture.html': 'ng-html2js'
  },

  ngHtml2JsPreprocessor: {
      stripPrefix: 'app/',
      // setting this option will create only a single module that contains templates
      // from all the files, so you can load them all with module('templates')
      moduleName: 'templates'
  },

次に、私が使用するテストで:

beforeEach(module('templates'));

ディレクティブの私の templateUrl は次のようになります。

templateUrl: 'scripts/directives/views/profile_picture.html',

取得

Error: Unexpected request: GET scripts/directives/views/profile_picture.html

これの何が問題なのか、何をチェックすべきか提案してください。

4

2 に答える 2

3

私は同じような問題を抱えていました。ファイルが完全に一致していることを確認してください。Google Chrome コンソールを開き、ファイル パスがまったく同じであることを確認します。

ここに画像の説明を入力

上の例では、「/」文字列を ngHtml2JsPreprocessor.stripPrefix に追加する必要があり、機能しました。

于 2014-07-31T15:00:23.107 に答える
0

@robharrisazは正しいです。テンプレートは raw JS として扱うことはできません。これは files 配列が期待するものです。テンプレートを囲むAngular の<script>ラッパーが必要です。

ここで期待しているのは、ファイル配列にテンプレートをリストすることで、テストの実行時にテンプレートがページに既に含まれていることです。ngMock モジュールの $httpBackend サービスを使用して、このファイルを取得するリクエストを処理していないと思います。これは、次の 2 つの方法のいずれかで修正できます。

  1. 別の方法でテンプレートをページに埋め込むことができます。URL を ID としてスクリプト タグでラップするだけです。

    <script type="text/ng-template" id="scripts/directives/views/profile_picture.html">
    ... YOUR TEMPLATE HTML HERE
    </script>
    
  2. ngMock を含めて $httpBackend を実装できます。このテンプレートの GET リクエストをトラップし、テンプレートを返します (または、テストしたい代わりのテンプレートを返します - これが $httpBackend のポイントです)。以下のようなテストスイートのコードでそれを行うことができます。

    'use strict';
    
    describe('Templates', function() {
        var $httpBackend;
    
        beforeEach(function () {
            module('myApp');
            module('myApp.whatever');
    
            // Set up HTTP overrides for things like the emoji.json assets
            inject(['$httpBackend', function(_httpBackend) {
                $httpBackend = _httpBackend;
    
                // We don't actually need real contents, this is just a unit test
                $httpBackend.whenGET(/(.*)/).respond(function(method, url, data, headers) {
                    // Return a sample template so we can see what the caller does with it
                    return [200, '<div>' + url + '</div>'];
                });
            }]);
        });
    
        describe('myTemplate', function() {
            it('should load myTemplate when we navigate to /profile-picture', inject(['$rootScope', '$location', function($rootScope, $location) {
                $httpBackend.flush();
    
                // Trigger your action here. We have a view manager that we ask to load our template here
    
                expect(something).toEqual('something');
            }]));
        });
    });
    
于 2014-06-20T14:12:45.017 に答える