Chutzpah の Visual Studio Test Adapter のようなヘッドレス ブラウザを取得して、ディレクティブが .html テンプレート ファイルにアクセスできるようにする方法を知っている人はいますか? Chutzpah は、私のオプションを制限しているように見えるヘッドレス ブラウザに PhantomJS を使用しています。
Chutzpah Test Adapter 2.5 と AngularJS 1.2.0-r.2 を使用しています。
エラーが発生します:
予期しない要求: GET myApp/directives/template.html
これは、Angular が $http サービスを使用してディレクティブのテンプレートにアクセスしようとしたことが原因です。
私はいくつかの異なる回避策を見つけました:
- XMLHttpRequest を使用して手動でテンプレートをインポートします。
- Grunt などのユーティリティを使用して、テンプレートをディレクティブの JS コードにインライン化します。
$httpBackend.when('GET', 'myApp/directives/template.html').passThrough()
- これは単体テストではなく、e2e テストでのみ機能します。- テンプレートをテスト コードに直接挿入します。
これらのオプションのどれも、私を特に満足させません。コンポーネントとしてテストできるように、ディレクティブがそのテンプレートを透過的にロードできるようにしたいと考えています。このシナリオを機能させる方法はありますか?
コード例:
angular.module('myDirective', []).directive('myDirective', function() {
return {
restrict: 'E',
transclude: true,
templateUrl: 'myApp/directives/template.html',
// Some other options, omitted for brevity.
};
});
template.html:
<div><div ng-transclude></div></div>
ジャスミンテストの例:
describe('myDirective', function() {
// Assign $scope using inject(), load module etc.
// Insert workaround for $httpBackend loading my templateUrl here.
it('should transclude content', function() {
var element = $compile("<my-directive>Look Mom, I'm in my directive!</my-directive>")($scope);
$scope.$digest();
expect(element.text().trim()).toBe("Look Mom, I'm in my directive!");
});
}