Skip to content

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

Open
@MinhLu12

Description

@MinhLu12

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions