Testing a backbone relation model by using jasmine
Asked Answered
A

3

11

Let's suppose I have two simple fixture files, one for the user(1) and one for the messages(2).

The Backbone Model for the messages is the following (3).

If I load the "Message Fixture", I would like to have also the related info regarding the user as specified in Message Model.
What is the proper way to active this goal in a spec view (4) by using jasmine test suite?
Please see the comments in (4) for more details.


(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();
            });
        });
    });
});
Alow answered 14/8, 2012 at 14:23 Comment(1)
This doesn't specifically answer your question -- and they're qunit -- but the specs for backbone-relational itself might help you: github.com/PaulUithol/Backbone-relational/blob/master/test/…Encomiast
M
1

Take a look at this series of tutorials http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html

This is the specific part about Model testing.

Don't know if will solve your problem, but may contain precious info.

Maladroit answered 24/8, 2012 at 15:4 Comment(1)
thanks for your answer. Actually the tutorial does not say anything about backbone-relational.Alow
C
0

this.fixtureResponse is the source data for the model, but when the model is actually created it makes a copy of that data to an internal property. So, when Backbone Relational resolves the relation, it shouldn't change the source data object.

Did you tried with expect(this.model.get('recipient_user')).toBeDefined()?

Crochet answered 1/12, 2012 at 22:17 Comment(0)
H
0

Backbone-Relational provides the ability to either create a related model from nested entities within JSON retrieved via the model's fetch or to lazily load related models using fetchRelated.

You're providing Backbone-Relational with the message model data but no way to retrieve the user model data. You could add another response returning the appropriate related user data and call fetchRelated on the message model.

Alternatively inline the user data into the message response and the user model will be created automatically and added as a relation on the message model.

Heterogeneous answered 26/2, 2013 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.