How does Chai-HTTP integrate with Sinon in this situation?

28 views
Skip to first unread message

Minh Lu

unread,
Oct 24, 2017, 2:24:30 AM10/24/17
to Sinon.JS
Here is the route I want to test:
```
app.post('/api/user', (req, res) => {

        dbService.replaceUserOnDuplicate(req.body, function returnResponse(insertedId) {
            if (insertedId === 0 || insertedId === req.body.id) {
                return res.sendStatus(200);                            
            }
            // TODO_MINH: Send an appropriate error to handle by the front-end
            return res.send({});
        });
    });
```
I can use chai-http to do something like this (psuedo-code):
```
it('test', function (done) {
            chai.request(server)
                .post('/api/user')
                .send({ user: SomeUserObject })
                .end(function (err, res) {
                    res.should.have.status(200);
                    done();
                });

        });
```
However, the api/users route makes a Database call. How would I use sinon to stub this method (replaceUserOnDuplicate) so that it returns a dummy response (like 0, or anything)?

Is it possible? I'm looking at the Chai-HTTP syntax and I see no room to insert any Sinon stubbed methods.

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages