-
-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: add 'properties' assertion #72
Comments
So to be clear, this would be sugar for expect(obj).to.have.property("a", 42);
expect(obj).to.have.property("b", "someVal"); ? |
Yes, exactly. |
I am not particularly sure that expect(obj).to.have.properties({
a: {
key: 'answer to life, the universe, and everything'
, value: 42
}
, b: [ 'hello', 'universe' ]
}); Your getting into the area of deep equality and it could be confusing to an end user as to which assertion to use. I like the concept, but I'm not sure if Let's leave this open for more discussion... Please chime in if you have any ideas. |
That's a good point. I guess in such a case I would expect the properties to be checked against the whole object tree, kind of like a big destructuring bind. For example: expect(obj).to.have.properties({
a: {
key1: 'val1',
key2: 'val2'
}
, b: [ 'hello', 'universe' ]
}); Would pass if given var obj = {
a: {
key1: 'val1',
key2: 'val2',
otherProperty: 'otherValue'
},
b: ['hello', 'happy', 'universe']
}; This would be pretty cool, since you could use it to make very succinct checks against the whole object tree. The array check is a little odd, since it's different from regular destructuring. But I think it makes sense in this context. Maybe I'm curious to know your thoughts on this; if it doesn't belong in core (which I could easily believe), do you think it can be easily added in a plugin? I had some trouble before trying to access the 'util' object, which seems necessary to write any new kind of assertion that doesn't build solely on existing ones. |
Plugin is probably the best way to go, and this is a great use case for one. In short, a plugin is no more than a function that can be chai.use(function (_chai, utils) {
// ...
}); We have found that the best way to implement is have a single To me, it sounds like your building an object diff function, then checking the diff to make sure all of the imbalance is of the additive nature. Well, that is how I would approach it. |
Did you find a solution to this? |
Not yet, my work has shifted to other areas for the moment. I'll re-approach it from the plugin perspective when I next deal with the JS part of my system. On Jun 18, 2012, at 8:44 AM, Jake Luer wrote:
|
Ok. Closing for now but feel free to re-open if you run into issues when you get there. |
As far as I understand - the problem is that there's advanced functional like My statistics - 95% of time I check for simple, not nested properties, and usually there are multiple properties i'm interested in. Basically the problem here as I see it: we can't add support for simple frequently used feature because it breaks complex, rarely used feature (but it's only my opinion, maybe I'm wrong). |
In your scenario, it has more to do with expect(obj).to.have.property({ one: 1, two: 2 }); Is this all encompassing... I only expect these two properties? If thats the case, then var obj = { one: [ 'a', 'b', 'c', 'd' ] };
expect(obj).to.have.property('one')
.an('array')
.with.length.above(3)
.and.include.keys('a', 'b'); Which means, we would be changing the behavior of an assertion without a flag. We want to leave that to "plugins only" as much as possible. Right now, if I am continually testing the structure of a similar object in several different tests, I write a helper plugin. If it's different objects, I agree that it can be tedious to write out 'to.have.property' many times if that is the situation.... But, I am still not convinced that |
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) |
|
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 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:
The text was updated successfully, but these errors were encountered: