Is there a jasmine matcher to compare objects on subsets of their properties
Jasmine 2.0
expect(result).toEqual(jasmine.objectContaining(example))
Since this fix: https://github.com/pivotal/jasmine/commit/47884032ad255e8e15144dcd3545c3267795dee0
it even works on nested objects, you just need to wrap each object you want to match partially in jasmine.objectContaining()
Simple example:
it('can match nested partial objects', function ()
{
var joc = jasmine.objectContaining;
expect({
a: {x: 1, y: 2},
b: 'hi'
}).toEqual(joc({
a: joc({ x: 1})
}));
});
I've had the same problem. I just tried this code, it works for me :
expect(Object.keys(myObject)).toContain('myKey');
I don't think it is that common and I don't think you can find one. Just write one:
beforeEach(function () {
this.addMatchers({
toInclude: function (expected) {
var failed;
for (var i in expected) {
if (expected.hasOwnProperty(i) && !this.actual.hasOwnProperty(i)) {
failed = [i, expected[i]];
break;
}
}
if (undefined !== failed) {
this.message = function() {
return 'Failed asserting that array includes element "'
+ failed[0] + ' => ' + failed[1] + '"';
};
return false;
}
return true;
}
});
});