如何断言 sinon 猫鼬存根

How to assert sinon mongoose stubs

有一个单元测试用例使用了一个被存根的猫鼬模型,我需要确保 authModel.create 被调用。如何断言?

我的单元测试:

     const createUserStub = sinon.stub(authModel, 'create')
            .resolves(userContent)

        return request(app).post('/auth/create')
            .send(userContent)
            .set('Accept', 'application/json')
            .expect((response) => {
                const apiResponse = JSON.parse(response.text)

                expect(createUserStub.calledOnce).to.equal(true)
                expect(apiResponse).to.be.an('Object')
                expect(apiResponse).to.have.property('_id')
                expect(apiResponse).to.have.property('name')
                expect(apiResponse).to.have.property('last_name')
                expect(response.res.statusCode).to.equal(200)
            }).end(done)

第一个 expect 断言总是 returns false。

您需要检查 called 属性 而不是 calledOnce(calledOnce 是 属性 的 sinon.spy )。

expect(createUserStub.called).to.equal(true)

应该可以。谢谢!