Can I make a stub for a range of outcomes? "sinon.stub().callsFake(()=>{})" AssertionError: NaN
Can I make a stub for a range of outcomes? "sinon.stub().callsFake(()=>{})" AssertionError: NaN
根据我的理解,存根是被迫采取行动的间谍,例如选择一方(即投掷)。我可以/或者假装返回一个数字范围是否有意义?
我正在尝试测试 "Loan.prototype.cost",它调用 "Loan.prototype.payment",然后进行一些计算。
在我的代码段的注释部分,我尝试创建一个数字范围("const rdnPaymentReturn" 从 85.61 到 85.63),并将 "payment" 属性 伪造为这个范围(const paymentSpy)。但我会收到错误提示“AssertionError: expected NaN to be within 27.32..27.56"
如果我 can/or 将 "paymentSpy" 设置为一个数字范围 (85.61-85.63) 是有意义的,我该如何解决这个 AssertionError?
参考:
beforeEach(function() {
l = new Loan();
});
describe('#cost()', function() {
it('should call #payment() and return the correct cost amount', function() {
l.principal = 1000;
l.term = 1;
l.rate = 0.05;
sinon.stub(l, 'payment').callsFake(() =>{ return 85.61; });
expect(l.cost()).to.equal(27.32);
// const rdnPaymentReturn = () =>{
// return Math.random() * (85.63 - 85.61) + 85.61;
// }
//const paymentSpy = sinon.stub(l, 'payment').callsFake(() =>{ return rdnPaymentReturn; });
//expect(l.cost()).to.be.within(27.32, 27.56);
});
});
这不太正确:
const paymentSpy = sinon.stub(l, 'payment').callsFake(() =>{ return rdnPaymentReturn; });
意思是:存根 payment
有一个函数,当调用时 return 是对函数 rdnPaymentReturn
的引用。但是 rdnPaymentReturn
永远不会被调用。
因此您需要 return 调用函数的结果:
const paymentSpy = sinon.stub(l, 'payment').callsFake(() => {
return rdnPaymentReturn()
});
或者直接传入函数调用:
const paymentSpy = sinon.stub(l, 'payment').callsFake(rdnPaymentReturn);
根据我的理解,存根是被迫采取行动的间谍,例如选择一方(即投掷)。我可以/或者假装返回一个数字范围是否有意义? 我正在尝试测试 "Loan.prototype.cost",它调用 "Loan.prototype.payment",然后进行一些计算。
在我的代码段的注释部分,我尝试创建一个数字范围("const rdnPaymentReturn" 从 85.61 到 85.63),并将 "payment" 属性 伪造为这个范围(const paymentSpy)。但我会收到错误提示“AssertionError: expected NaN to be within 27.32..27.56"
如果我 can/or 将 "paymentSpy" 设置为一个数字范围 (85.61-85.63) 是有意义的,我该如何解决这个 AssertionError?
参考:
beforeEach(function() {
l = new Loan();
});
describe('#cost()', function() {
it('should call #payment() and return the correct cost amount', function() {
l.principal = 1000;
l.term = 1;
l.rate = 0.05;
sinon.stub(l, 'payment').callsFake(() =>{ return 85.61; });
expect(l.cost()).to.equal(27.32);
// const rdnPaymentReturn = () =>{
// return Math.random() * (85.63 - 85.61) + 85.61;
// }
//const paymentSpy = sinon.stub(l, 'payment').callsFake(() =>{ return rdnPaymentReturn; });
//expect(l.cost()).to.be.within(27.32, 27.56);
});
});
这不太正确:
const paymentSpy = sinon.stub(l, 'payment').callsFake(() =>{ return rdnPaymentReturn; });
意思是:存根 payment
有一个函数,当调用时 return 是对函数 rdnPaymentReturn
的引用。但是 rdnPaymentReturn
永远不会被调用。
因此您需要 return 调用函数的结果:
const paymentSpy = sinon.stub(l, 'payment').callsFake(() => {
return rdnPaymentReturn()
});
或者直接传入函数调用:
const paymentSpy = sinon.stub(l, 'payment').callsFake(rdnPaymentReturn);