BlockchainDeveloper
BlockchainDeveloper

Reputation: 520

Mocha + Chai + Sinon stub- TypeError: Cannot read property 'set' of undefined

In nodejs I am in an express controller that has the response object doing this as the second statement:

res.set('Content-Type', 'application/json');

When I try to stub it out

sinon.stub(contractorController.putcontractor, 'set').resolves(true);

I get the error

TypeError: Cannot stub non-existent own property

I've tried many things, please help. This controller is being imported with a require statement and the controller itself is a series of exported functions (No classes). I have been able to get this to work when I create classes, but I am not certain I am supposed to be refactoring all this code into classes just so the unit tests will work.

Upvotes: 1

Views: 2192

Answers (1)

BlockchainDeveloper
BlockchainDeveloper

Reputation: 520

In express while unit testing the req and res objects with all their related methods both need to be stubbed.

    const res = {
        set: sinon.stub(),
        get: sinon.stub(),
        ...
       }

Upvotes: 1

Related Questions