Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting a backbone relation model by using jasmine
    text
    copied!<p>Let's suppose I have two simple fixture files, one for the user(1) and one for the messages(2). </p> <p>The Backbone Model for the messages is the following (3). </p> <p>If I load the "Message Fixture", I would like to have also the related info regarding the user as specified in Message Model.<br> What is the proper way to active this goal in a spec view (4) by using jasmine test suite?<br> Please see the comments in (4) for more details.</p> <hr> <p>(1)</p> <pre><code>// 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' } ] } } } }); }); </code></pre> <hr> <p>(2)</p> <pre><code>// 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" } ] } } } }); }); </code></pre> <hr> <p>(3)</p> <pre><code>// 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 } ] }); </code></pre> <hr> <p>(4)</p> <pre><code>// 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(); }); }); }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload