Ade*_*lin 4 angularjs karma-runner
我和我的老板就Angular E2E测试进行了热烈的讨论.根据vojitajina pull请求我们需要运行服务器才能运行e2e测试.因此,运行e2e测试涉及真实服务器,真正的服务器涉及DB.这使得测试变慢.哦,现在的问题是如何在不涉及真实服务器的情况下测试e2e?有没有办法使用httpBackend和e2e angular API我可以使用browser(),element(),select(),我的测试?
[请参阅下面的编辑]我们使用shell脚本定期捕获来自种子测试服务器的curl请求.然后通过$ httpBackend.whenGet(...)返回这些响应.response()拦截并返回该数据.
所以,在我们的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>');
}
Run Code Online (Sandbox Code Playgroud)
ourTest.js
angular.module('ourTestApp', ['ngMockE2E','taskMocks']).
run(function ($httpBackend, taskMocks) {
$httpBackend.whenGET(/views\/.*/).passThrough();
$httpBackend.whenGET(/\/fixtures.*\//).passThrough();
taskMocks.register();
$httpBackend.whenGET(/.*/).passThrough();
});
Run Code Online (Sandbox Code Playgroud)
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);
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
在少数情况下,我们的数据与当前日期有关; 例如"本周的任务",所以我们在register()方法中按下捕获的数据.
编辑 我们现在使用Rosie为我们的模拟对象创建工厂.因此,没有更多的CURLing测试服务器用于预期的json响应.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);
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1958 次 |
| 最近记录: |