How can I assert that all elements of list are objects?
should.exist(list)
list.should.be.an('array')
... // ?
If we are talking about should
, you can use such method:
> var should = require('./');
> var list1 = [1, 2, 3];
> var list2 = [1, { a: 10}, 3];
> var list3 = [{ a: 11}, {b: 10}];
> list1.should.matchEvery(function(it) { return it.should.be.an.Object(); });
AssertionError: expected Array [ 1, 2, 3 ] to match each Function { name: '' }
expected 1 to match Function { name: '' }
expected 1 to be an object
expected 1 to have type object
expected 'number' to be 'object'
at Assertion.fail (/Users/den/Projects/shouldjs/should.js/lib/assertion.js:180:17)
at Assertion.prop.value (/Users/den/Projects/shouldjs/should.js/lib/assertion.js:65:17)
...
> list2.should.matchEvery(function(it) { return it.should.be.an.Object(); });
AssertionError: expected Array [ 1, Object { a: 10 }, 3 ] to match each Function { name: '' }
expected 1 to match Function { name: '' }
expected 1 to be an object
expected 1 to have type object
expected 'number' to be 'object'
at Assertion.fail (/Users/den/Projects/shouldjs/should.js/lib/assertion.js:180:17)
at Assertion.prop.value (/Users/den/Projects/shouldjs/should.js/lib/assertion.js:65:17)
...
> list3.should.matchEvery(function(it) { return it.should.be.an.Object(); });
{ obj: [ { a: 11 }, { b: 10 } ],
anyOne: false,
negate: false,
params:
{ operator: 'to match each Function { name: \'\' }',
message: undefined } }
>
So i have used .matchEvery
and checked that each element is object. You can refer to api docs for more examples.