2013-05-07 49 views
2

你如何设置你的测试,以便使用新的Ember测试助手,如visitfind。我有一些困难,使其工作。使用Ember测试助手的设置是什么?

我试过如下:在我的规格 Ember.testing = true

App.setupForTesting()

App.injectTestHelpers()

然后我得到一个消息,说我

创建命名空间之前将需要包装任何异步鳕鱼e与Ember.run所以我做了,但我的夹具数据(嘲笑GET请求)不知何故无法访问,所以我不能设置数据给他们。

你想通了吗?

+1

我刚刚发布了关于这个主题的截屏视频http://toranbillups.com/blog/archive/2013/07/21/Integration-testing-your-emberjs-app-with-QUnit-and-Karma/ – 2013-07-22 03:54:49

回答

4

假设你正在使用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