mardi 4 juillet 2017

Promise rejected or fulfilled confusion

I have a promise function

create(data) {
        return new Promise((resolve, reject) => {
            const datetime = Date.parse(new Date());
            data.createdAt = datetime;
            data.updatedAt = datetime;
            this._db.collection(this._table).insertOne(data, (err) => {
                if (err) {
                    this._logger.error(err);
                    reject(err);
                } else {
                    resolve(data);
                }
            });
        });
    }

And write unit test for it

it('should reject insertion', () => {
    const promise = Agent.create(data).then(() => (Agent.create(data)));
    return expect(promise).to.be.rejected;
});

This assertion passes. But if the unit test is:

it('should reject insertion', () => {
    const promise = Agent.create(data).then(Agent.create(data));
    return expect(promise).to.be.rejected;
});

It fails. The error is AssertionError: expected promise to be rejected but it was fulfilled with { Object (_id, role, ...) }

Where is the difference between these two practices? I haven't gotten it.

Aucun commentaire:

Enregistrer un commentaire