what i'd do
describe('my object', function() { it('has these properties', function() { expect(object.keys(myobject)).toequal([ 'property1', 'property2', ... ]); }); }); but of course object.keys returns array, definition ordered...i'd prefer have test pass regardless of property ordering (which makes sense me since there no spec object key ordering anyway...(at least es5)).
how can verify object has properties supposed have, while making sure isn't missing properties, without having worry listing properties in right order?
you use _.has (which wraps myobject.hasownproperty(prop)):
var _ = require('underscore'); describe('my object', function() { it('has these properties', function() { var props = [ 'property1', 'property2', ... ]; props.foreach(function(prop){ expect(_.has(myobject, prop)).tobetruthy(); }) }); }); edit forgot built in now!
describe("jasmine.objectcontaining", function() { var foo; beforeeach(function() { foo = { a: 1, b: 2, bar: "baz" }; }); it("matches objects expect key/value pairs", function() { expect(foo).toequal(jasmine.objectcontaining({ bar: "baz" })); expect(foo).not.toequal(jasmine.objectcontaining({ c: 37 })); }); });
Comments
Post a Comment