Angular E2E テストについて、私と上司の間で熱い議論が交わされました。vojitajina プル リクエストによると、e2e テストを実行するにはサーバーを実行する必要があります。したがって、e2e テストの実行には実サーバーが関与し、実サーバーには DB が関与します。これにより、テストが遅くなります。問題は、実際のサーバーを使用せずに e2e をテストする方法です。httpBackend を使用して、テストに browser()、element()、select() を使用できる e2e angular API を使用する方法はありますか?
2 に答える
[以下の編集を参照] シェル スクリプトを使用して、シードされたテスト サーバーから定期的に curl リクエストをキャプチャします。これらの応答は、$httpBackend.whenGet(...).respond() を介して返され、そのデータをインターセプトして返します。
したがって、index.html で
if (document.location.hash === '#test') {
addScript('/test/lib/angular-mocks.js');
addScript('/test/e2e/ourTest.js');
window.fixtures = {};
addScript('/test/fixtures/tasks/tasks_p1.js');
// the tasks_p1.js is generated and has "window.fixtures.tasks_p1 = ...json..."
addScript('/test/fixtures/tasks/tasks_p2.js');
// the tasks_p2.js is generated and has "window.fixtures.tasks_p2 = ...json..."
addScript('/test/e2e/tasks/taskMocks.js\'><\/script>');
}
私たちのTest.js
angular.module('ourTestApp', ['ngMockE2E','taskMocks']).
run(function ($httpBackend, taskMocks) {
$httpBackend.whenGET(/views\/.*/).passThrough();
$httpBackend.whenGET(/\/fixtures.*\//).passThrough();
taskMocks.register();
$httpBackend.whenGET(/.*/).passThrough();
});
taskMocks.js
/*global angular */
angular.module('taskMocks', ['ngMockE2E']).
factory('taskMocks', ['$httpBackend', function($httpBackend) {
'use strict';
return {
register: function() {
$httpBackend.whenGET(..regex_for_url_for_page1..).respond(window.fixtures.tasks_p1);
$httpBackend.whenGET(..regex_for_url_for_page2..).respond(window.fixtures.tasks_p2);
}
};
}]);
いくつかのケースでは、データが現在の日付に関連しています。たとえば、「今週のタスク」なので、register() メソッドでキャプチャされたデータを処理します。
編集Rosieを 使用して、モック オブジェクトのファクトリを作成します。そのため、予期される json 応答に対して CURLing テスト サーバーを使用する必要はありません。index.html はこれらの ".js" フィクスチャをロードしなくなり、モックは次のようになります。
taskMocks.js
/*global angular */
angular.module('taskMocks', ['ngMockE2E']).
factory('taskMocks', ['$httpBackend', function($httpBackend) {
'use strict';
var tasks_p1 = [ Factory.build('task'), Factory.build('task')],
tasks_p2 = [ Factory.build('task'), Factory.build('task')]
return {
register: function() {
$httpBackend.whenGET(..regex_for_url_for_page1..).respond(tasks_p1);
$httpBackend.whenGET(..regex_for_url_for_page2..).respond(tasks_p2);
}
};
}]);
あなたの質問に答えるには、はい、実際のサーバーを使用せずにそれを行う方法があり、$httpBackend を使用して e2e テストで応答をモックできますが、単体テストでそれらをモックするほど簡単ではありません。また、index.html に angular-mocks.js を含め、app.js に「ngMockE2E」を含める必要があります。