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

时间:2013-07-26 23:07:05

标签: ember.js tdd integration-testing ember-testing

我在使用Toran Billup的TDD guide与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");
        });
});

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

2 个答案:

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

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;
    }
});

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);
    });
});

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;

答案 1 :(得分:4)

我对Karma不熟悉,但是你需要与ember交互的测试部分应该被推入运行循环中(正如你所提到的那样)

Ember.run.next(function(){
  //do somethin
  transition stuff here etc
});

要检查当前路由,你可以从ember中窃取信息,这里是我在某些时候从堆栈溢出中窃取的一些信息。

var router = App.__container__.lookup("router:main"); //get the main router
var currentHandlerInfos = router.router.currentHandlerInfos; //get all handlers
var activeHandler = currentHandlerInfos[currentHandlerInfos.length - 1]; // get active handler
var activeRoute = activeHandler.handler; // active route

如果您开始进行控制器测试,我会在http://discuss.emberjs.com/t/unit-testing-multiple-controllers-in-emberjs/1865

上写下一些信息