2013-07-26 14 views
8

Sto riscontrando un problema durante i test di integrazione con ember utilizzando TDD guide di Toran Billup.Come eseguire il test di integrazione Ember per le transizioni di instradamento?

Sto usando Karma come mio runner di test con Qunit e Phantom JS.

Sono sicuro che la metà di se ha a che fare con la mia conoscenza da principiante del runloop Ember. La mia domanda è in 2 parti:

1) Come faccio a racchiudere correttamente un test di vist() nel ciclo di esecuzione?

2) Come è possibile testare le transizioni? Il percorso dell'indice ('/') dovrebbe transitare in una rotta di risorse chiamata '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"); 
     }); 
}); 

Grazie in anticipo per eventuali indicatori nella giusta direzione.

+0

Quale versione di ember stai usando? Non ho menzionato un bug critico in RC 6 che si interrompe quando si visita il percorso "/" https://github.com/emberjs/ember.js/issues/2997 –

+0

Ah! Sto usando R6.1! Scusate, ho dimenticato di menzionare nella mia domanda. Grazie mille per il vostro aiuto! – ganicus

risposta

7

ho appena spinto di un esempio di applicazione che fa una semplice transizione quando si colpisce il "/" ember.js percorso utilizzando RC5

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

la semplice "ciao mondo" esempio si presenta

1.) il Te mplate si reindirizzati durante la transizione

<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.) il codice dell'applicazione 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.) il test di integrazione appare così

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.) l'helper di integrazione che utilizzo sulla maggior parte dei miei progetti 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

Sono familiarità con il Karma, ma le porzioni del test che ha bisogno di interagire con brace deve essere inserito nel circuito corsa (come v'è stato citano)

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

Per controllare il percorso attuale è possibile rubare informazioni fuori dal campo, ecco alcune informazioni che ho rubato dall'overflow dello stack ad un certo punto.

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 

Se si inizia a fare test di controllo, ho scritto un po 'di informazioni che http://discuss.emberjs.com/t/unit-testing-multiple-controllers-in-emberjs/1865

+0

Ember.run.next() ha eseguito il mio test senza errori, ma ricevo un messaggio di errore che indica che non ci sono state asserzioni? Adoro l'esempio precedente riguardo l'accesso al percorso corrente per ottenere informazioni aggiornate sui gestori. Ne giocherò sicuramente nei miei test! – ganicus

Problemi correlati