假設我有兩個簡單的夾具文件,一個用於用戶(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();
});
});
});
});
這並沒有明確地回答你的問題 - 他們是qunit - 但骨幹關係本身的規格可能會幫助你:https://github.com/PaulUithol/Backbone-relational/blob/master/ test/tests.js#L534 – 2012-09-22 18:05:00