0

If I comment the line return Promise.reject(err); in my findGood function, the test case should fail. Meaning that, if I'm not returning from my function, the catch block in the test should be undefined.

var findGood = function (name) {

    return goodModel.findByName(name)
        .then( (result) => {

            ....
            return result;


        })
        .catch( (err) => {          
            return Promise.reject(err);
        }); 
};

Here is a test

it('good not found', function () {

   var goodModelStub = sinon.stub(goodModel, 'findByName');

   var error = 'Good not found';

   goodModelStub.returns(Promise.reject(error));

   return goodFinder.findGood('Sony')
     .catch(function (err) {
         assert.equal(err, error);
  });

});

Mathlight
  • 6,436
  • 17
  • 62
  • 107
Ricky ponting
  • 4,537
  • 4
  • 13
  • 26
  • `.catch( (err) => Promise.reject(err))` is pretty much a useless statement, as you'd get the exact same behavior if you just removed it. – Frxstrem Feb 22 '17 at 12:20
  • @Frxstrem then what should i do? can you pls answer if you know? – Ricky ponting Feb 22 '17 at 12:21
  • you must call the promise, like real, not directly to catch http://stackoverflow.com/questions/26571328/how-do-i-properly-test-promises-with-mocha-and-chai – Álvaro Touzón Feb 22 '17 at 12:22

1 Answers1

1

First there's no need to catch an error and then throw it again, handle the rejection in the proper place (your test case in this instance)

findGood returns a Promise so why not use chai-as-promised

it('good not found', function () { 
   var goodModelStub = sinon.stub(goodModel, 'findByName');

   var error = 'Good not found';

   goodModelStub.returns(Promise.reject(error));

   return goodFinder.findGood('Sony').should.be.rejected
  });
});
Yoni Levy
  • 1,562
  • 2
  • 16
  • 35