2012-08-21 18 views
18

Sto tentando di implementare un test (1) per questo modulo (2).
Il mio scopo è verificare se la raccolta viene recuperata quando viene attivato un particolare evento.
Come potete vedere dal mio commento in (2) ottengo il messaggio Error: Expected a spy, but got Function.
Il modulo funziona ma il test fallisce. qualche idea?È prevista una spia, ma ha ottenuto la funzione


(1)

// jasmine test module 

describe('When onGivePoints is fired', function() { 
    beforeEach(function() { 
     spyOn(this.view.collection, 'restartPolling').andCallThrough(); 
     app.vent.trigger('onGivePoints'); 
    }); 
    it('the board collection should be fetched', function() { 
     expect(this.view.collection.restartPolling).toHaveBeenCalled(); 
     // Error: Expected a spy, but got Function. 
    }); 
}); 

(2)

// model view module 
return Marionette.CompositeView.extend({ 
    initialize: function() { 
     this.collection = new UserBoardCollection(); 
     this.collection.startPolling(); 
     app.vent.on('onGivePoints', this.collection.restartPolling); 
    }, 
    // other code 
}); 
+0

non c'è abbastanza codice per vedere cosa sta succedendo. per favore includi più che solo le singole funzioni - includi la definizione dell'oggetto a cui appartengono le funzioni e il codice che istanzia gli oggetti, almeno. –

+0

@DerickBailey grazie per il tuo tempo. Ho aggiornato la mia domanda con il codice di modalità. –

+0

Io uso QUnit piuttosto che Jasmine, ma la tua chiamata ad app.vent.trigger non dovrebbe essere nel metodo "it" piuttosto che prima di Each? – codemonkey

risposta

32

è necessario per ottenere nel metodo attuale, che in questo caso è sul prototipo.

describe('When onGivePoints is fired', function() { 
    beforeEach(function() { 
     spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough(); 
     app.vent.trigger('onGivePoints'); 
    }); 
    it('the board collection should be fetched', function() { 
     expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled(); 
    }); 
}); 

Spiare il prototipo è un bel trucco si può usare quando non è possibile ottenere l'istanza effettiva che si desidera spiare.

2

Avevo anche lo stesso problema ma l'ho risolto passando un argomento nella chiamata di funzione. Poi si deve scrivere il vostro banco di prova simile in it

var data = {name:"test"} 
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough(); 
UsersBoardCollection.prototype.restartPolling(data); 
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled(); 
+0

grazie ha funzionato per me bene !!! –

-2

ho avuto questo problema perché ho avuto due versioni di Sinon caricati, o forse non ero l'inizializzazione Sinon-gelsomino correttamente. Quando ho caricato esplicitamente sinon e poi sinon gelsomino nella configurazione della mia specifica, ha iniziato a funzionare correttamente.

Problemi correlati