gpt4 book ai didi

javascript - 如何测试对象的可选成员?

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:06 25 4
gpt4 key购买 nike

测试可选对象成员的最佳技术是什么。现在我们在期望语句前面加上一个 if:

 if(object.member) expect(object).to.have.a.property('member').that.is.a('string');

但是必须有一种在风格上更加内联的方法。例如

 expect(object).to.have.an.optional.property('member').that.is.a('string');

或(为了便于阅读,添加为空链):

 expect(object).to.have.an.optional.property('member').that.would.be.a('string');

或者(移动可选以提供expect的替代版本):

 optionally.expect(object).to.have.a.property('member').that.is.a('string');
<小时/>

更新 - 我开始编写这段代码(chai 新手),看看我是否可以实现我的目标,所以我添加了一个小插件:

module.exports = function(chai, utils) {
var Assertion = chai.Assertion
, i = 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;
});
};

使用情况:

it('could be null or have a 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();
})

当前的问题是,即使属性不存在,函数的控制也会结束 - 但评估链仍在继续。我需要在没有失败断言的情况下结束评估 - 这可能吗?

<小时/>

更新任一解决方案(简单的解决方案):

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

我认为实现有点笨拙 - 但粗箭头函数可能会削弱一些语法。所以就在这里等待!

最佳答案

The current problem even when the property isn't present, the control of the function ends - but the evaluation chain continues. I need to end the evaluation with out a failing assertion - is this possible?

在阅读了 chai 的 pluginguide我会使用类似的方法来设置标志。然而,我得出了同样的结论——你不能简单地停止一条链条。

我想到的一种可能性不仅是实现新属性和新标志,而且覆盖 assert方法本身 - 当 OPTIONAL_FLAG 时不抛出当前标记Assertion对象已设置。然而,破坏一切或错过边缘情况的机会太高。

毕竟,我认为这不是一个好主意。引用自this "confusing syntax" issue :

I think the misunderstanding comes from your expectation that Chai follows most/all English grammar rules. Unfortunately, English grammar has way too many rules (and exceptions to those rules) for it to be a reasonable undertaking to implement.

The challenge with designing Chai's chain-able assertions is finding the balance between being expressive and concise. Even if full grammar wasn't a daunting task to implement and document, it would make the API less concise, which is not good for a testing environment.

RULE: Any "flag" which modifies the behavior of an assertion (negation not or inclusion include/contain , etc...), once set to true should remain true until the end of the chain.

这意味着不可能实现像.or这样的东西。也是运算符。

但可能的是实现类似的东西

either(function(){
expect(object).not.to.have.a.property('member');
}).or(function(){
expect(object).to.have.a.property('member').that.is.a('string');
});

也许可以在此基础上构建一种更具吸引力的语法。

关于javascript - 如何测试对象的可选成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com