javascript - How to Test an optional member of an object? -
what best technique test optional object member. right prefacing expect statements if:
if(object.member) expect(object).to.have.a.property('member').that.is.a('string');
but there must method more inline stylistically. e.g.
expect(object).to.have.an.optional.property('member').that.is.a('string');
or (adding empty chain, readability):
expect(object).to.have.an.optional.property('member').that.would.be.a('string');
or (moving optional provide alternative version of expect):
optionally.expect(object).to.have.a.property('member').that.is.a('string');
update - started write code (new chai) see if accomplish targeting, added small plugin:
module.exports = function(chai, utils) { var assertion = chai.assertion , = utils.inspect , flag = utils.flag; var optional_flag = 'chai-optional/option' assertion.addproperty('optional', function() { flag(this, optional_flag, true) return this; }) assertion.overwritemethod('property', function (_super) { return function assertproperty (propertyname) { if (flag(this, optional_flag)) { flag(this, optional_flag, false) ; var obj = this._obj; var ispropertypresent = (obj[propertyname]) ? true : false ; if(ispropertypresent) { return _super.apply(this, arguments); } } else { _super.apply(this, arguments); } }; }); assertion.addproperty('would', function () { return this; }); };
with usage:
it('could null or have value', function(done){ var objwithout = {} var objwith = {} objwith.someproperty = 'blah' expect(objwith).to.have.optional.property('someproperty').that.would.be.a('string'); expect(objwithout).to.have.optional.property('someproperty').that.would.be.a('string'); return done(); })
the current problem when property isn't present, control of function ends - evaluation chain continues. need end evaluation out failing assertion - possible?
update either solution (simplistic solution):
var either = function(firstcondition){ var returnobject = {} try{ firstcondition() returnobject.or = function(secondcondition){ return } } catch(e) { returnobject.or = function(secondcondition){ return secondcondition() } } return returnobject ; } module.exports = either
i think implementation little clunky - fat arrow functions hap thin out of syntax. here waiting on that!
the current problem when property isn't present, control of function ends - evaluation chain continues. need end evaluation out failing assertion - possible?
after having read chai's plugin guide have used similar approach flag. however, have reached same conclusion - cannot stop chain.
a possibility though of not implement new properties , new flag, overwrite assert
method - not throw when optional_flag
flag on current assertion
object set. however, chance destroy or miss edge case hight.
after all, don't think it's idea. citing this "confusing syntax" issue:
i think misunderstanding comes expectation chai follows most/all english grammar rules. unfortunately, english grammar has way many rules (and exceptions rules) reasonable undertaking implement.
the challenge designing chai's chain-able assertions finding balance between being expressive , concise. if full grammar wasn't daunting task implement , document, make api less concise, not testing environment.
rule: "flag" modifies behavior of assertion (negation not or inclusion include/contain , etc...), once set true should remain true until end of chain.
this means impossible implement .or
operator well.
what possible though implement like
either(function(){ expect(object).not.to.have.a.property('member'); }).or(function(){ expect(object).to.have.a.property('member').that.is.a('string'); });
maybe 1 can build more appealing syntax on that.
Comments
Post a Comment