更新:
我有一个简单的骨干关系模型:
@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)
为什么?该应用程序运行正确,测试不正确。
因此错误为ReferenceError: product is not defined
。这是因为您在测试中的before
函数和中定义了它,所以两者不相同,并且在测试中它是未定义的。你必须在你的描述块中定义它:
"严格使用"
描述"产品",->product=空
beforeEach ->
product = new App.M.Product
it 'should be defined', ->
expect(product).toBeDefined()
对于另一个错误TypeError: Cannot read property 'prototype' of undefined
,您似乎错过了在collectionType
下添加提及的集合。因此,插件正在全局名称空间中查找App.C.Pages
,并试图获得对象的原型。但正如错误所说,它没有定义