mreq
mreq

Reputation: 6542

Testing backbone-relational with jasmine

UPDATED:

I have a simple Backbone-relational model:

@App.M.Product = Backbone.RelationalModel.extend
    defaults: {}
    url: App.D.dataURL
    relations: [
        {
            type: Backbone.HasMany
            key: 'templates'
            relatedModel: 'App.M.Template'
            collectionType: 'App.C.Templates'
            reverseRelation:
                key: 'product'
        }
        {
            type: Backbone.HasMany
            key: 'pages'
            relatedModel: 'App.M.Page'
            collectionType: 'App.C.Pages'
            reverseRelation:
                key: 'product'
        }
        {
            type: Backbone.HasMany
            key: 'uploaded_images'
            relatedModel: 'App.M.UploadedImage'
            collectionType: 'App.C.UploadedImages'
            reverseRelation:
                key: 'product'
        }
    ]
    initialize: ->
        if @get('minimumNumberOfPages') < 1
            @set 'minimumNumberOfPages', 1
    fixPages: ->
        col = @get 'pages'
        col.each (page) ->
            page.collection = col

and an even simplier test suite:

"use strict"

describe 'Product', ->
    pr = null
    beforeEach ->
        pr = new App.M.Product
    it 'should be defined', ->
        expect(pr).toBeDefined()

However, the model fails to initialize (taken from testem's terminal output):

TypeError: Cannot read property 'prototype' of undefined
    at Backbone.HasMany.Backbone.Relation.extend.initialize (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:835:78)
    at Backbone.Relation (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:537:9)
    at new child (http://localhost:7357/app/bower_components/backbone/backbone-min.js:1:25220)
    at _.extend.initializeRelation (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:137:5)
    at null.<anonymous> (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:1176:31)
    at Array.forEach (native)
    at Function.w.each.w.forEach (http://localhost:7357/app/bower_components/underscore/underscore-min.js:1:599)
    at Backbone.RelationalModel.Backbone.Model.extend.initializeRelations (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:1175:6)
    at Backbone.RelationalModel.Backbone.Model.extend.set (http://localhost:7357/app/bower_components/backbone-relational/backbone-relational.js:1373:11)
    at Backbone.Model (http://localhost:7357/app/bower_components/backbone/backbone-min.js:1:3929)

Why is that? The app is wokring correctly, the tests are not.

Upvotes: 0

Views: 511

Answers (1)

Andreas K&#246;berle
Andreas K&#246;berle

Reputation: 110922

So the error is ReferenceError: product is not defined. This cause you defined it in the before function and in the test, so both are not the same and in the test its undefined. You have to define it in your describe block :

"use strict"

describe 'Product', -> product = null

beforeEach ->
    product = new App.M.Product

it 'should be defined', ->
    expect(product).toBeDefined()

For the other error TypeError: Cannot read property 'prototype' of undefined, it seems you have missed to add the collections mention under collectionType. So the plugin is looking for App.C.Pages in the global namespace and and try to get the prototype of the object. But as the error says its not defined

Upvotes: 1

Related Questions