【问题标题】:Testing models default value测试模型默认值
【发布时间】:2014-03-20 22:05:31
【问题描述】:

以下模型的所有属性默认值为 null。但是当我使用 Jasmines toBeNull() 函数测试属性时,它不会评估为真。并说属性未定义。为什么?
我定义了以下主干模型:

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?

【问题讨论】:

    标签: unit-testing backbone.js jasmine marionette


    【解决方案1】:

    你不能直接访问模块的属性:

    groupModel.gid;
    

    您应该改用accessors

    groupModel.get('gid');
    

    在您的情况下,您有 groupModel.gid == null,因为 groupModel.gidundefined 并且在 JS 中:

    undefined == null // => true
    null == null // => true
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2018-10-22
      • 1970-01-01
      • 2021-11-30
      • 2010-12-26
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多