2012-08-14 94 views
11

假設我有兩個簡單的夾具文件,一個用於用戶(1),另一個用於消息(2)。使用jasmine測試骨幹關係模型

消息的骨幹模型如下(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/tests.js#L534 – 2012-09-22 18:05:00

回答

0

this.fixtureResponse是模型的源數據,但是當模型實際創建時,它會將該數據的副本作爲內部屬性。所以,當Backbone Relational解析關係時,它不應該改變源數據對象。

您是否試過expect(this.model.get('recipient_user')).toBeDefined()

0

Backbone-Relational提供了通過模型獲取的JSON中的嵌套實體創建相關模型或使用fetchRelated延遲加載相關模型的功能。

您正在使用消息模型數據提供Backbone-Relational,但無法檢索用戶模型數據。您可以添加另一個響應,返回適當的相關用戶數據並在消息模型上調用fetchRelated。

或者將用戶數據內聯到消息響應中,並且用戶模型將自動創建並添加爲消息模型中的關係。