使用Ember测试助手的设置是什么?

时间:2013-05-07 12:53:07

标签: ember.js

如何设置测试以使用新的Ember测试助手,例如visitfind。我有一些困难让它发挥作用。

我尝试了以下内容:

创建命名空间之前: Ember.testing = true

在我的规范中:

App.setupForTesting()

App.injectTestHelpers()

然后我收到一条消息,说我需要用Ember.run包装任何异步代码,所以我做了,但我的夹具数据(模拟GET请求)在某种程度上是不可访问的,因此我无法为它们设置数据。 / p> 你弄清楚了吗?

1 个答案:

答案 0 :(得分: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

相关问题