Supponiamo di avere due semplici file di fissaggio, uno per l'utente (1) e uno per i messaggi (2).Test di un modello di relazione backbone utilizzando il gelsomino
Il modello Backbone per i messaggi è il seguente (3).
Se carico il "Message Fixture", mi piacerebbe avere anche le informazioni relative all'utente come specificato nel Modello messaggio.
Qual è il modo corretto di attivare questo obiettivo in una vista delle specifiche (4) utilizzando la suite di test jasmine?
Vedere i commenti in (4) per ulteriori dettagli.
(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();
});
});
});
});
Questo non risponde in modo specifico alla tua domanda - e sono qunit - ma le specifiche per lo stesso backbone relazionale potrebbero aiutarti: https://github.com/PaulUithol/Backbone-relational/blob/master/ test/tests.js # L534 –