假设你正在使用RC5(不RC6,只是还没有,因为它有一个轻微的错误)
document.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
Ember.testing = true;
App.rootElement = '#ember-testing';
App.setupForTesting();
App.injectTestHelpers();
function exists(selector) {
return !!find(selector).length;
}
function stubEndpointForHttpRequest(url, json) {
$.mockjax({
url: url,
dataType: 'json',
responseText: json
});
}
$.mockjaxSettings.logging = false;
$.mockjaxSettings.responseTime = 0;
然后你会写你的测试,像这样
module('integration tests', {
setup: function() {
App.reset();
App.Person.people = [];
},
teardown: function() {
$.mockjaxClear();
}
});
test('ajax response with 2 people yields table with 2 rows', function() {
var json = [{firstName: "x", lastName: "y"}, {firstName: "h", lastName: "z"}];
stubEndpointForHttpRequest('/api/people', json);
visit("/").then(function() {
var rows = find("table tr").length;
equal(rows, 2, rows);
});
});
这里是一个完全成熟的示例项目显示如何添加上面显示的集成帮手和一些基本的集成测试(使用karma/qunit/jquery-mockjax)
https://github.com/toranb/ember-testing-example
我刚刚发布了关于这个主题的截屏视频http://toranbillups.com/blog/archive/2013/07/21/Integration-testing-your-emberjs-app-with-QUnit-and-Karma/ – 2013-07-22 03:54:49