Timigen
Timigen

Reputation: 1025

Testing models default value

The model below has null for all its attributes defaults. But when I test the attributes with Jasmines toBeNull() function it doesnt evaluate to true. And says the attribute is undefined. Why?
I have the following backbone model defined :

Entities.GroupModel = Backbone.Model.extend({
    defaults: 
{
    "gid"         : null,
    "title"       : null,
    "description" : null,
    "access_date" : null
}
});

Then in a test I create a new model like so...

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?

Upvotes: 0

Views: 73

Answers (1)

Dmytro Yarmak
Dmytro Yarmak

Reputation: 1018

You can't access attributes of module directly as:

groupModel.gid;

You should use accessors instead:

groupModel.get('gid');

In your case you have groupModel.gid == null because groupModel.gid is undefined and in JS:

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

Upvotes: 3

Related Questions