如何进行路由转换的Ember集成测试?

gan*_*cus 8 tdd integration-testing ember.js ember-testing

我在使用Toran Billup的TDD指南与ember进行集成测试时遇到了问题.

我使用Karma作为Qunit和Phantom JS的测试运行员.

我肯定有一半与初学者对Ember runloop的了解有关.我的问题是两部分:

1)如何正确地将vist()测试包装到运行循环中?

2)我如何测试过渡?索引路由('/')应该转换为名为'projects.index'的资源路由.

module("Projects Integration Test:", {
  setup: function() {
    Ember.run(App, App.advanceReadiness);
  },
  teardown: function() {
    App.reset();
  }
});

test('Index Route Page', function(){
    expect(1);
    App.reset();    
        visit("/").then(function(){
            ok(exists("*"), "Found HTML");
        });
});
Run Code Online (Sandbox Code Playgroud)

提前感谢任何指向正确的方向.

Tor*_*ups 7

我刚刚推送了一个示例应用程序,当您使用ember.js RC5点击"/"路径时,该应用程序执行简单的转换

https://github.com/toranb/ember-testing-example

简单的"hello world"示例如下所示

1.)您在转换期间重定向到的模板

<table>
{{#each person in controller}}
<tr>
  <td class="name">{{person.fullName}}</td>
  <td><input type="submit" class="delete" value="delete" {{action deletePerson person}} /></td>
</tr>
{{/each}}
</table>
Run Code Online (Sandbox Code Playgroud)

2.)ember.js应用程序代码

App = Ember.Application.create();

App.Router.map(function() {
    this.resource("other", { path: "/" });
    this.resource("people", { path: "/people" });
});

App.OtherRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('people');
    }
});

App.PeopleRoute = Ember.Route.extend({
    model: function() {
        return App.Person.find();
    }
});

App.Person = Ember.Object.extend({
    firstName: '',
    lastName: ''
});

App.Person.reopenClass({
    people: [],
    find: function() {
        var self = this;
        $.getJSON('/api/people', function(response) {
            response.forEach(function(hash) {
                var person = App.Person.create(hash);
                Ember.run(self.people, self.people.pushObject, person);
            });
        }, this);
        return this.people;
    }
});
Run Code Online (Sandbox Code Playgroud)

3.)集成测试看起来像这样

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);
    });
});
Run Code Online (Sandbox Code Playgroud)

4.)我在大多数ember.js项目中使用的集成助手

document.write('<div id="foo"><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;
Run Code Online (Sandbox Code Playgroud)