0

更新:

我有一个简单的骨干关系模型:

@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

还有一个更简单的测试套件:

"use strict"

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

但是,模型无法初始化(取自 testem 的终端输出):

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)

这是为什么?该应用程序工作正常,测试不正常。

4

1 回答 1

1

所以错误是ReferenceError: product is not defined。这导致您在before函数测试中定义了它,因此两者都不相同,并且在测试中未定义。您必须在您的描述块中定义它:

“使用严格”

描述“产品”,-> 产品 = null

beforeEach ->
    product = new App.M.Product

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

对于另一个错误TypeError: Cannot read property 'prototype' of undefined,您似乎错过了添加下面提到的集合collectionType。所以插件在全局命名空间中寻找App.C.Pages并尝试获取对象的原型。但正如错误所说,它没有定义

于 2013-07-01T08:48:34.743 回答