3

e2e シナリオのディレクティブでいくつかの基本的なテストを行おうとしています。コードは問題なく動作し、新しい要素をブラウザーにレンダリングできます。ここで私が使用しているコード。

ここにディレクティブコード。

'use strict';

var directives = angular.module('lelylan.directives', [])

directives.directive('device', ['Device', function(Device) {
  var definition = {
    restrict: 'E',
    replace: true,
    templateUrl: 'js/templates/device.html',
    scope: { id: '@' }
  };

  definition.link = function postLink(scope, element, attrs) {
    scope.$watch('id', function(value){
      var device = Device.get({ id: scope.id }, function() {
        scope.device = device;
      });
    });
  };

  return definition
}]);

ここでは、デバイス サービス コードです。

// Service Device
'use strict';

angular.module('lelylan.services', ['ngResource']).
  factory('Device', ['$resource', '$http', function($resource, $http) {

    var token = 'df39d56eaa83cf94ef546cebdfb31241327e62f8712ddc4fad0297e8de746f62';
    $http.defaults.headers.common["Authorization"] = 'Bearer ' + token;

    var resource = $resource(
      'http://localhost:port/devices/:id',
      { port: ':3001', id: '@id' },
      { update: { method: 'PUT' } }
    );

    return resource;
  }]);

ここにアプリコード。

'use strict';

angular.module('lelylan', ['lelylan.services', 'lelylan.directives'])

ここに index.html があります。

<!doctype html>
<html lang="en" ng-app="lelylan">
  <head>
    <meta charset="utf-8">
    <title>Lelylan Components</title>
    <link rel="stylesheet" href="css/app.css"/>
  </head>
  <body>

    <device id="50c61ff1d033a9b610000001"></device>

    <!-- In production use: <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script> -->
    <script src="lib/angular/angular.js"></script>
    <script src="lib/angular/angular-resource.js"></script>
    <script src="js/app.js"></script>
    <script src="js/services.js"></script>
    <script src="js/directives.js"></script>
  </body>
</html>

Angular のドキュメントを読み、さまざまなソリューションを試した後、バックエンド リクエストをモックする次のテストを思いつきました。問題は、リクエストがまだ実際のサービスにヒットすることです。リクエストを傍受できないようです。

// e2e test
'use strict';

describe('directives', function() {

  var resource = { id: '1', uri: 'http://localhost:3001/devices/1' };
  var myAppDev = angular.module('myAppDev', ['lelylan', 'ngMockE2E']);

  myAppDev.run(function($httpBackend) {
    $httpBackend.when('GET', 'http://localhost:3001/devices/1').respond(resource);
    $httpBackend.when('GET', /\/templates\//).passThrough();
  });

  beforeEach(function() {
    browser().navigateTo('../../app/index.html');
  });

  describe('when renders a device', function() {

    it('renders the title', function() {
      expect(element('.device .name').text()).toEqual('Closet dimmer');
    });

    it('renders the last time update', function() {
      expect(element('.device .updated-at').text()).toEqual('2012-12-20T18:40:19Z');
    })
  });
});

いくつかの構成が欠けていると思いますが、どの構成が本当に理解できません。どうもありがとう。

4

1 に答える 1

1

この質問の最後のコメントを読んで、最終的な解決策を見つけました。

実は、モック化されたアプリケーションを使用する HTML ファイルを使用する必要があったため、重要なステップが 1 つ欠けていました。コードが話せるようにしましょう。

1) テスト環境で HTML ファイルを作成しました。主な違いは、ng-app=test を設定し、2 つの新しい JS ファイルを追加したことに関連しています。1 つ目は /test/e2e/app-test.js で、テスト モジュールを作成した場所で、2 つ目は /test/lib/angular-mocks.js です。

<!doctype html>
<html lang="en" ng-app="test">
  <head>
    <meta charset="utf-8">
    <title>Lelylan Test</title>
    <link rel="stylesheet" href="css/app.css"/>
  </head>
  <body>

    <device id="1"></device>

    <script src="lib/angular/angular.js"></script>
    <script src="lib/angular/angular-resource.js"></script>
    <script src="js/app.js"></script>
    <script src="js/settings.js"></script>
    <script src="js/services.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/filters.js"></script>
    <script src="js/directives.js"></script>

    <!-- Test application with mocked requests -->
    <script src="../test/e2e/app-test.js"></script>
    <script src="../test/lib/angular/angular-mocks.js"></script>
  </body>
</html>

では、テスト モジュールの実装方法を見てみましょう。ここでは、$httpBackend にアクセスして HTTP リクエストをモックできるようにする ngMockE2E を追加して、メイン モジュール (lelylan) とまったく同じモジュールを定義します。

'use strict';

var resource = { id: '1', uri: 'http://localhost:3001/devices/1' };

var test = angular.module('test', ['lelylan', 'ngMockE2E']);

test.run(function($httpBackend) {
  $httpBackend.when('GET', 'http://localhost:3001/devices/1').respond(resource);
  $httpBackend.when('GET', /\/templates\//).passThrough();
});

これ以上何もない。scripts/e2e-test.sh を実行すれば完了です。

于 2013-02-01T09:23:25.687 に答える