2013-07-26 10 views
8

Toran BillupのTDD guideを使用してemberとの統合テストに問題があります。ルートトランジションのEmber統合テストを行うにはどうすればよいですか?

私はKunaをQunitとPhantom JSのテストランナーとして使用しています。

初心者のEmberランループの知識と関係があるのは確かです。私の質問は2部です:

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

正しい方向のポインタについては、事前におねがいします。

+0

どのバージョンのemberを使用していますか?私は "/"ルートにアクセスしたときに壊れるRC 6の重大なバグに言及しなかった。https://github.com/emberj/ember.js/issues/2997 –

+0

ああ!私はR6.1を使用しています!申し訳ありませんが、私は私の質問で言及するのを忘れました。あなたの助けをありがとう! – ganicus

答えて

7

にいくつかの情報を書きましたRC5

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

簡単な "Hello World" の例では、この

1のように見える)、TEあなたは、トランジション

<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)のように見えるの間にリダイレクトmplate私の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; 
4

私はカルマに慣れていないんだけど、(あなたが言及したように)燃えさしと対話する必要があなたのテストの一部が実行ループの中に押し込まれるべき

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

することができます現在のルートを確認するには私はどこかでスタックオーバーフローから盗んだ情報があります。

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 

あなたは、コントローラのテストをやって起動した場合、私はあなたがember.jsを使用して「/」ルートを打ったとき、私はちょうど簡単な移行を行うサンプルアプリケーションを押し上げているhttp://discuss.emberjs.com/t/unit-testing-multiple-controllers-in-emberjs/1865

+0

Ember.run.next()はエラーなしで実行するテストを取得しましたが、アサーションがないことを示すFailメッセージが表示されます。上の例ではハンドラの現在の情報を得るために現在のルートをタップするのが大好きです。私は間違いなく私のテストでそれを周遊します! – ganicus

関連する問題