2012-08-14 13 views
11

का उपयोग कर रीढ़ की हड्डी संबंध मॉडल का परीक्षण करना मान लीजिए कि मेरे पास दो सरल स्थिरता फ़ाइलें हैं, एक उपयोगकर्ता (1) और संदेशों के लिए एक (2)।जैस्मीन

संदेशों के लिए बैकबोन मॉडल निम्न (3) है।

यदि मैं "संदेश स्थिरता" लोड करता हूं, तो मैं संदेश मॉडल में निर्दिष्ट उपयोगकर्ता के बारे में संबंधित जानकारी भी लेना चाहता हूं।
चमेली परीक्षण सूट का उपयोग करके एक स्पेक व्यू (4) में इस लक्ष्य को सक्रिय करने का सही तरीका क्या है?
अधिक जानकारी के लिए कृपया (4) में टिप्पणियां देखें।


(1)

// User Fixture 
beforeEach(function() { 
    this.fixtures = _.extend(this.fixtures || {}, { 
     Users: { 
      valid: { 
       status: 'OK', 
       version: '1.0', 
       response: { 
        users: [ 
         { 
          id: 1, 
          name: 'olivier' 
         }, 
         { 
          id: 2, 
          name: 'pierre', 
         }, 
         { 
          id: 3, 
          name: 'george' 
         } 
        ] 
       } 
      } 
     } 
    }); 
}); 

(2)

// Message Fixture 
beforeEach(function() { 
    this.fixtures = _.extend(this.fixtures || {}, { 
     Messages: { 
      valid: { 
       status: 'OK', 
       version: '1.0', 
       response: { 
        messages: [ 
         { 
          sender_id: 1, 
          recipient_id: 2, 
          id: 1, 
          message: "Est inventore aliquam ipsa" 
         }, 
         { 
          sender_id: 3, 
          recipient_id: 2, 
          id: 2, 
          message: "Et omnis quo perspiciatis qui" 
         } 
        ] 
       } 
      } 
     } 
    }); 
}); 

(3)

// Message model 

MessageModel = Backbone.RelationalModel.extend({ 
    relations: [ 
     { 
      type: Backbone.HasOne, 
      key: 'recipient_user', 
      keySource: 'recipient_id', 
      keyDestination: 'recipient_user', 
      relatedModel: UserModel 
     }, 
     { 
      type: Backbone.HasOne, 
      key: 'sender_user', 
      keySource: 'sender_id', 
      keyDestination: 'sender_user', 
      relatedModel: UserModel 
     } 
    ] 
}); 

(4)

// Spec View 

describe('MyView Spec', function() { 
     describe('when fetching model from server', function() { 
      beforeEach(function() { 
       this.fixture = this.fixtures.Messages.valid; 
       this.fixtureResponse = this.fixture.response.messages[0]; 
       this.server = sinon.fakeServer.create(); 
       this.server.respondWith(
        'GET', 
        // some url 
        JSON.stringify(this.fixtureResponse) 
       ); 
      }); 
      it('should the recipient_user be defined', function() { 
       this.model.fetch(); 
       this.server.respond(); 
       // this.fixtureResponse.recipient_user is not defined 
       // as expected by the relation defined in (3) 
       expect(this.fixtureResponse.recipient_user).toBeDefined(); 
      }); 
     }); 
    }); 
}); 
+0

यह विशेष रूप से आपके प्रश्न का उत्तर नहीं देता है - और वे qunit हैं - लेकिन बैकबोन-रिलेशनल के लिए चश्मे स्वयं आपकी मदद कर सकते हैं: https://github.com/PaulUithol/Backbone-relational/blob/master/ परीक्षण/test.js # L534 –

उत्तर

1

ट्यूटोरियल http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html

की इस श्रृंखला पर एक नजर डालें इस Model testing के बारे में विशेष हिस्सा है।

पता नहीं है कि आपकी समस्या का समाधान होगा या नहीं, लेकिन इसमें बहुमूल्य जानकारी हो सकती है।

+0

आपके उत्तर के लिए धन्यवाद। दरअसल ट्यूटोरियल रीढ़ की हड्डी से संबंधित कुछ भी नहीं कहता है। –

0

this.fixtureResponse मॉडल के लिए स्रोत डेटा है, लेकिन जब मॉडल वास्तव में बनाया जाता है तो यह उस डेटा की एक प्रतिलिपि को आंतरिक संपत्ति में बनाता है। इसलिए, जब बैकबोन रिलेशनल संबंध को हल करता है, तो उसे स्रोत डेटा ऑब्जेक्ट को नहीं बदला जाना चाहिए।

क्या आपने expect(this.model.get('recipient_user')).toBeDefined() के साथ प्रयास किया था?

0

बैकबोन-रिलेशनल या तो मॉडल के fetch के माध्यम से जेएसओएन के भीतर नेस्टेड इकाइयों से संबंधित मॉडल बनाने या fetchRelated का उपयोग करके आलसी लोड मॉडल से संबंधित मॉडल बनाने की क्षमता प्रदान करता है।

आप संदेश मॉडल डेटा के साथ बैकबोन-रिलेशनल प्रदान कर रहे हैं लेकिन उपयोगकर्ता मॉडल डेटा को पुनर्प्राप्त करने का कोई तरीका नहीं है। आप उचित संबंधित उपयोगकर्ता डेटा को वापस लौटने और संदेश मॉडल पर लाने के लिए एक और प्रतिक्रिया जोड़ सकते हैं।

वैकल्पिक रूप से उपयोगकर्ता डेटा को संदेश प्रतिक्रिया में रेखांकित करें और उपयोगकर्ता मॉडल स्वचालित रूप से बनाया जाएगा और संदेश मॉडल पर एक संबंध के रूप में जोड़ा जाएगा।

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