2015-01-27 8 views
5

Ho un inizializzatore stdlib. In quell'inizializzatore, riapro alcune delle classi di Ember incorporate e aggiungo alcuni metodi personalizzati. Ad esempio, ho aggiunto reverseEach a Ember.Enumerable e Array.Utilizzo di un inizializzatore in un test di unità?

Funzionano bene nell'app, ma nei test ricevo "reverseEach: non definito non è una funzione".

Come si indica che il test deve utilizzare l'inizializzatore?

Ho provato needs: [..., 'initializer:stdlib']. Non inciampa su questo, ma continuo a ricevere l'errore "indefinito".

Ecco un test di esempio:

`import { test, moduleForModel } from 'ember-qunit'` 

moduleForModel 'foo', 'foo', 
    needs: [ 
    'model:bar' 
    'initializer:stdlib' 
    ] 

test 'deleteLastNItems', -> 
    model = @subject() 
    model.set '', ['foo', 'bar', 'baz', 'quux'] 
    model.deleteLastNItems 2 # This should not die with "reverseEach: undefined is not a function" 
    deepEquals model.get('someProperty'), ['foo', 'bar'] 

risposta

-1

Si può usare ember g initializer stdlib, verrà generato il codice di esempio per voi, tra cui test.

import Ember from 'ember'; 
import { initialize } from '../../../initializers/stdlib'; 
import { module, test } from 'qunit'; 

var registry, application; 

module('Unit | Initializer | stdlib', { 
    beforeEach: function() { 
    Ember.run(function() { 
     application = Ember.Application.create(); 
     registry = application.registry; 
     application.deferReadiness(); 
    }); 
    } 
}); 

// Replace this with your real tests. 
test('it works', function(assert) { 
    initialize(registry, application); 

    // you would normally confirm the results of the initializer here 
    assert.ok(true); 
}); 

consultare: Ember blueprints

+1

La questione non è come verificare l'inizializzazione in sé, ma il modo di testare un'altra unità (e g modello..), Che utilizza metodi scimmia-patch dal inizializzatore. –

+0

it, puoi estrarre il metodo con patch scimmia su un Ember.mixin, quindi importare il Mixin al posto dell'inizializzatore. – zhenhua

+0

Io sono una scimmia che rattoppa Ember stessa, non la modella. Ad esempio, aggiungo un nuovo metodo a 'Ember.MutableArray': un metodo' .destructiveMap() 'che modifica l'array dato piuttosto che restituire un nuovo array. –

Problemi correlati