vendredi 23 novembre 2018

Can I make a stub for a range of outcomes? "sinon.stub().callsFake(()=>{})" AssertionError: NaN

From my understanding, a stub is a spy who is forced to take action, such as picking a side(i.e. throw). Can I /or does it makes sense to fake returning a range of number? I am trying to test a "Loan.prototype.cost", which calls "Loan.prototype.payment", then does some calculation.

In the commented out part of my snippet, I tried to create a range of number(the "const rdnPaymentReturn" from 85.61 to 85.63), and fake the "payment" property to be this range(const paymentSpy). But I would get error saying " AssertionError: expected NaN to be within 27.32..27.56"

If I can/or it makes sense to set "paymentSpy" to a range of number(85.61-85.63), how do I resolve this AssertionError?

Ref:

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);            
        });
    });

Aucun commentaire:

Enregistrer un commentaire