2012-08-21 18 views
18

मिला है मैं इस मॉड्यूल (2) के लिए एक परीक्षण (1) को लागू करने की कोशिश कर रहा हूं।
मेरा उद्देश्य यह जांचना है कि कोई विशेष ईवेंट ट्रिगर होने पर संग्रह प्राप्त किया जाता है या नहीं।
जैसा कि आप मेरी टिप्पणी से देख सकते हैं (2) मुझे संदेश Error: Expected a spy, but got Function.
मॉड्यूल काम करता है लेकिन परीक्षण विफल रहता है। कोई विचार?एक जासूस की उम्मीद है, लेकिन फंक्शन


(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

क्या हो रहा है यह देखने के लिए पर्याप्त कोड नहीं है। कृपया केवल व्यक्तिगत कार्यों से अधिक शामिल करें - ऑब्जेक्ट परिभाषा को शामिल करें जिसमें फ़ंक्शंस हैं, और कोड जो कम से कम ऑब्जेक्ट्स को तुरंत चालू करता है। –

+0

@DerickBailey आपके समय के लिए धन्यवाद। मैंने मोड कोड के साथ अपना प्रश्न अपडेट किया। –

+0

मैं जैस्मीन के बजाए क्विनिट का उपयोग करता हूं, लेकिन ऐप.वेंट.ट्रिगर को आपका कॉल पहले से पहले "इसे" विधि में नहीं होना चाहिए? – codemonkey

उत्तर

32

आप वास्तविक विधि, जो इस मामले में प्रोटोटाइप पर है में प्राप्त करने की आवश्यकता।

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

प्रोटोटाइप पर जासूसी एक अच्छा चाल आप उपयोग कर सकते हैं जब आप वास्तविक उदाहरण आप पर जासूसी करना चाहते हैं नहीं मिल सकता है।

2

मुझे भी एक ही समस्या मिल रही थी, लेकिन मैंने फ़ंक्शन कॉल में तर्क पारित करके इसे हल किया। तो फिर तुम it

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

धन्यवाद मेरे लिए ठीक काम किया !!! –

-2

मैं इस बग था क्योंकि मैं लोड सिनोन के दो संस्करणों था, या संभवतः मैं सही ढंग से नहीं किया गया था initialising सिनोन-चमेली में इस तरह अपने परीक्षण का मामला लिखने के लिए की है। जब मैंने स्पष्ट रूप से साइनऑन लोड किया और फिर मेरे spec सेटअप में sinon jasmine, यह सही ढंग से चलना शुरू कर दिया।

संबंधित मुद्दे