Skip to content

Instantly share code, notes, and snippets.

@brihogan
Created July 20, 2012 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brihogan/3153178 to your computer and use it in GitHub Desktop.
Save brihogan/3153178 to your computer and use it in GitHub Desktop.
A chai extension: method that'll let you test backbone model properties (in a collection) much easier than explicitly testing each property one-at-a-time.
# Create a chai helper method
chai.use (_chai, utils) ->
# Backbone collection testing of a model for particular attributes.
#
# The first key in the hash must be the index number of the model in the
# collection. The value of the index are the properties you want
# to check. Example--- 1: {title:'Root'}
#
# @param properties [hash] index#:{key:'value'}
#
# @example
# collection.should.have.modelAttributes 1: title:'Root'
# collection.should.have.modelAttributes
# 1: title:'Root', next:true
# 2: title:'child', next:false
utils.addMethod chai.Assertion.prototype, 'modelAttributes', (properties) ->
# Get object from chai
collection = utils.flag this, 'object'
# Check each index
for index of properties
# Iterate over each key:value in each index
for key of properties[index]
# make sure that task.at(#) exists
expect(collection.at(index)).to.exist
# create a shortcut to atrributes
attrs = collection.at(index).attributes
# Then check to see if property exists
expect(attrs).to.have.property key
# Finally, see if property has value
# task at # should have key:val
expect(attrs).to.have.property key, properties[index][key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment