Automattic / expect.js

Minimalistic BDD-style assertions for Node.JS and the browser.
2.11k stars 209 forks source link

"Sub" object checking? #33

Open aseemk opened 12 years ago

aseemk commented 12 years ago

Hey there,

Great work on the library -- I really like it.

In writing tests against a REST API, I'd find it really convenient to check that response objects, which can have many properties and values, contain some particular properties and values.

@visionmedia's should.js has include() for this kind of thing, and it's really helpful:

https://github.com/visionmedia/should.js#includeobj

Here it might be e.g.:

expect(resp).to.include({
  name: 'John Smith',
  username: 'johnsmith',
  age: 29
});

But whereas should's include() is for checking presence in arrays and strings too, I see that expect has contain() for those. So maybe this could be added into contain(), but maybe not.

What do you think? Thanks!

piuccio commented 11 years ago

+1 I'd be nice to have

var resp = {
  one : 1,
  two : 2,
  three : 3
};

expect(resp).to.contain({
  one: 1,
  two : 2
});  // true
expect(resp).not.to.contain({
  three: "not a number"
});  // true (.not)

It's a more readable way of saying

expect(resp).to.have.keys("one", "two");
expect(resp.one).to.be(1);
expect(resp.two).to.be(2);
skeggse commented 10 years ago

@piuccio how about adding an expect().to.have.properties() assertion? This follows from expect().to.have.property(name, [value]), just with the ability to provide an object to properties().