2012-08-21 15 views
18

aldık.
Amacım belli bir olay tetiklendiğinde koleksiyon getirilen olup olmadığını kontrol etmektir.
(2) mesaj Error: Expected a spy, but got Function.
modül eserler olsun ama Test başarısız sen benim yorum görebileceğiniz gibi. herhangi bir fikir?bir casus Beklenen ama bu modülün (2) için bir test (1) uygulamak çalışıyorum İşlevini


(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

neler görmek için yeterli kod yok. Lütfen sadece tekil işlevlerden daha fazlasını dahil edin - fonksiyonların ait olduğu nesne tanımını ve nesneleri en az gösteren nesneyi ekleyin. –

+0

@DerickBailey Zaman ayırdığınız için teşekkür ederiz. Sorumu mod koduyla güncelledim. –

+0

"o" yönteminden çok beforeEach olmak Doğrusu Jasmine daha QUnit kullanırsınız, ancak çağrı app.vent.trigger için gerekmez mi? – codemonkey

cevap

32

Bu durumda prototip üzerinde gerçek usulde, içine almak gerekir. prototip üzerinde Casusluk

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

Eğer casusluk istediğiniz gerçek örneğine alamayan kullanabileceğiniz güzel bir hile.

2

Ben de aynı sorunu başlamıştı ama işlev çağrısında bir argümanı geçirerek bunu çözüldü. O zaman ben yüklü sinon iki sürümü, ya da muhtemelen ben doğru sinon-yasemin başlatılıyor değildi çünkü bu hata vardı it

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

Teşekkürler benim için iyi çalıştı! –

-2

böyle test durumda yazmak zorunda. Özel olarak sinon ve sinon jasmine özel olarak yüklediğimde, düzgün çalışmaya başladı.

İlgili konular