我有这样的功能:
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;
},
我的报道说“启用”部分没有涉及。如何解决这个问题
答案 0 :(得分:1)
要涵盖'enabled'
的情况,您只需要一个x[prop]
是真实的测试用例即可。
一种简单的方法是使用'toString'
之类的东西,因为Object.prototype.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
});