jeudi 23 avril 2015

SquireJS is throws error when mocking Backbone.RelationalModel

I am using SquireJS and Mocha to test some code that uses Backbone and Backbone-relational. I am getting the following error when trying to mock an object that contains a Backbone.RelationalModel constructor:

TypeError: this.set is not a function at Backbone.Model (path/to/backbone.js:256:10)

Here is a simplified example of my source code(source.js):

define([ './myObjDependency'], function(myObj){
    return Backbone.RelationalModel.extend({
        relations: [
            {
                type: Backbone.HasOne,
                key: 'myModel',
                relatedModel: myObj.Model
            }
        ]
    });
});

Then in my tests, I have this:

require(['Squire'], function(Squire) {
    var injector = new Squire();
    squire = injector
        .mock("./myObjDependency", {
            Model: Backbone.RelationalModel.extend({})
        });

    squire.require(['./source'], function (src) {
        var srcModelInstance = new src();
        // ... more code here ... //
    });
});

It seems like the Backbone.RelationalModel is calling its parent constructor (Backbone.Model), and is somehow giving it the incorrect this context.

Here is the full stack-trace in case it is helpful:

TypeError: this.set is not a function
at Backbone.Model (/path/to/_bower/components/backbone.js:256:10)
at Backbone.RelationalModel.Backbone.Model.extend.constructor (/path/to/_bower/components/backbone-relational.js:1225:20)
at child [as relatedModel] (/path/to/_bower/components/backbone.js:1566:41)
at Function._.result (/path/to/_bower/components/underscore.js:1168:40)
at Backbone.Relation (/path/to/_bower/components/backbone-relational.js:604:26)
at new child (/path/to/_bower/components/backbone.js:1566:41)
at _.extend.initializeRelation (/path/to/_bower/components/backbone-relational.js:185:15)
at null.<anonymous> (/path/to/_bower/components/backbone-relational.js:1305:31)
at Array.forEach (native)
at Function._.each._.forEach (/path/to/_bower/components/underscore.js:79:11)

Does anyone have any ideas on how to fix this or what the root cause of the problem is? Thanks.

Aucun commentaire:

Enregistrer un commentaire