以下模型的所有属性默认值均为null。但是当我使用Jasmines toBeNull()函数测试属性时,它不会评估为true。并说该属性未定义。为什么呢?
我定义了以下骨干模型:
Entities.GroupModel = Backbone.Model.extend({
defaults:
{
"gid" : null,
"title" : null,
"description" : null,
"access_date" : null
}
});
然后在测试中我创建了一个像这样的新模型......
var groupModel = new CCDocUploader.Entities.GroupModel({});
console.log(groupModel); ///when I inspect the attributes i see they are set to null
console.log(groupModel.gid == null); //this evaluates to true
expect(groupModel.gid).toBeNull(); ///this claims it is undefined and fails...why?
答案 0 :(得分:3)
您无法直接访问模块的属性:
groupModel.gid;
您应该使用accessors代替:
groupModel.get('gid');
在你的情况下,你有groupModel.gid == null
,因为groupModel.gid
是undefined
而在JS中:
undefined == null // => true
null == null // => true