dimanche 11 août 2019

Replace the value of a variable when testing a function

I have a route that records user data and gives a unique identifier.

router.post('/', (req, res) => {
    const user_email = req.body.user_email;
    const username = req.body.username;
    const user_phone = req.body.user_phone;
    const service_sid = serviceSidVerification(user_phone);
    const entry_id = uuidv4();
    const user = new User(
        username,
        user_email,
        user_password,
        user_phone,
        entry_id,
        service_sid,
    );
    await db.query(user.setUser());
    return res.status(200).json({
        entry_id: entry_id,
    });
});

I want to write a test for this route. I need to replace the return value from the setUser() function with my own. For example, 12345. How can this be done with a test?

This is what my test looks like.

describe(`Users Test`, () => {
    describe('POST /api/users/', () => {
        it(`Should create user`, (done) => {
            chai
                .request(server)
                .post('/api/users/')
                .send({ 'username': faker.internet.userName() })
                .send({ 'user_email': faker.internet.email() })
                .send({ 'user_password': faker.internet.password() })
                .send({ 'user_phone': faker.phone.phoneNumber() })
                .end((err, res) => {
                    expect(res).have.status(200);
                    expect(res.body).have.property('entry_id');
                    expect(res.body.entry_id).to.be.an('string');
                    done();
                });
            });
        });
    });
});

Aucun commentaire:

Enregistrer un commentaire