Skip to content Skip to sidebar Skip to footer

How Can I Make A Promise Reject For Testing?

I have a db.js set up to do all my database calls. This is an example of one of the functions that query the database. db.getUserCount = function () { return new Promise (function

Solution 1:

Make db.users.count() call to fail by either causing some change in database entry or in your api.

Solution 2:

I ended up using sinon stubs so the other test would still pass.

describe('db.getUserCount rejection test', function() {
    sinon.stub(db, 'getUserCount').returns(Q.reject(newError(errorMessage)));

    it('should be rejected when called', function() {
        return db.getUserCount().should.be.rejected;    
    });

    it.only('getUserCount responds with correct error message', function() {
        return db.getUserCount().catch(function(err) {
            expect(err.message).to.equal('Error could not connect to database');
        });

    });
});    

Post a Comment for "How Can I Make A Promise Reject For Testing?"