Javascript 如何在mocha/sinon中测试条件表达式
我有一些类似这样的功能:Javascript 如何在mocha/sinon中测试条件表达式,javascript,mocha.js,sinon,sinon-chai,Javascript,Mocha.js,Sinon,Sinon Chai,我有一些类似这样的功能: abc(prop) { const x = aComplexFunction(this.productData, 'productStatus'); let result; /* istanbul-ignore-next */ if (x) { const key = (x[prop]) ? 'enabled' : 'notEnabled'; result = `wayOfLife.${
abc(prop) {
const x = aComplexFunction(this.productData, 'productStatus');
let result;
/* istanbul-ignore-next */
if (x) {
const key = (x[prop]) ? 'enabled' : 'notEnabled';
result = `wayOfLife.${key}`;
}
return result;
},
我的保险范围说“已启用”部分不包括在内。如何修复此问题以覆盖
“启用的”
情况您只需要一个测试用例,其中x[prop]
是真实的
一个简单的方法是使用类似于'toString'
的东西,因为toString
存在于每个对象上,因为函数和函数的求值都是真实的
下面是一个稍微简化的工作示例,用于演示:
const expect = require('chai').expect;
const aComplexFunction = () => ({});
function abc(prop) {
const x = aComplexFunction();
let result;
if (x) {
const key = (x[prop]) ? 'enabled' : 'notEnabled';
result = `wayOfLife.${key}`;
}
return result;
};
it('will cover the enabled case', function() {
expect(abc('propThatDoesNotExist')).to.equal('wayOfLife.notEnabled'); // <= covers notEnabled
expect(abc('toString')).to.equal('wayOfLife.enabled'); // <= covers enabled
});
const expect=require('chai')。expect;
常量aComplexFunction=()=>({});
职能abc(道具){
常数x=aComplexFunction();
让结果;
if(x){
常量键=(x[prop])?“已启用”:“已注释”;
结果=`wayOfLife.${key}`;
}
返回结果;
};
它('将涵盖启用的情况',函数(){
expect(abc('propThatDoesNotExist')).to.equal('wayOfLife.notEnabled')//