question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: add 'properties' assertion

See original GitHub issue

There’s a pretty common case that the existing deep.equal, deep.property, and contains.keys APIs don’t cover, namely asserting that an object should have some properties of some given values. Namely, I’d like to be able to say:

obj = {...};
expect(obj).to.have.properties({
  a: 42, 
  b: "someVal"
});

obj is allowed to have other properties as well, but fails if a or b is absent or has a different value.

Here’s the coffeescript I hacked up to do this locally, which barely works:

chai.Assertion.addMethod 'properties', (expectedPropertiesObj) ->
  args = ([key,val] for own key,val of expectedPropertiesObj)
  args = _.flatten(args)
  this.property.apply(this, args)

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Zammycommented, Jan 22, 2015

Is there plugin that does this? I just realized I have written wrong tests (they were passing although they were not doing anything). Maybe add another base test like “.properties”. I am not experienced with chai but maybe you need feedback from people like me. 😃 I now need to write .property for each of the properties I want to check.

(Update1) It seems that http://chaijs.com/plugins/chai-subset is doing what I want. (Update2) Of course I cannot make it work with chai-things sigh.

0reactions
eps1loncommented, Oct 1, 2020

to.include works exactly like you want:

When the target is an object, .include asserts that the given object val’s properties are a subset of the target’s properties.

expect({a: 1, b: 2, c: 3}).to.include({a: 1, b: 2});

https://www.chaijs.com/api/bdd/#includeval-msg

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property Content Assertions | ReadyAPI Documentation
The Message Content assertion checks the content of messages and properties according to complex rules. This assertion is deprecated. Use the Smart Assertion...
Read more >
Assertion TestStep | Functional Testing - SoapUI
Use the Add Assertion wizard to easily select and configure assertions to be included in the Assertion TestStep. After selecting the Source, the...
Read more >
Add or Remove XML Element(s) Assertion - TechDocs
If you are removing an element, specify the context variable that contains a reference to remove a specific signature or other element(s). This ......
Read more >
Can I add annotations to object property assertion from jena?
Jena, though it has some support for OWL via OntModels, is really an RDF-based API. Statements in Jena are just a class representing...
Read more >
Automating Response Validation With Assertions In Postman
This is a powerful feature to create really robust & fast integration tests. ... #4) Now, let's try adding a test to this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found