javascript - 如何断言 sinon mongoose 存根
问题描述
拥有一个使用 mongoose 模型的单元测试用例,我需要确保调用了 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)
在第一个期望断言总是返回 false。
解决方案
您需要检查被调用的属性而不是 calledOnce ( calledOnce 是 sinon.spy 的属性)。
expect(createUserStub.called).to.equal(true)
应该有效。谢谢!
推荐阅读
- typescript - 如何在 forge 模型衍生 API 中定义衍生缩略图的图像大小?
- excel - 使用 Excel VBA 的集合数的笛卡尔积
- dialogflow-es - 如何从 MySQL 数据库中获取数据到 Dialogflow?
- laravel - Laravel Nova 在 Select 字段中获取所选项目的值
- android - ADB 不断断开未在 Mac OSX 上显示的设备
- c# - 如何将模型列表转换为 json highchart 地图数据
- julia - 如何获取给定类型的最大值和最小值
- reactjs - 反应:无法读取未定义的属性“部分”
- ios - 是否可以像照片库一样在 iOS 中选择人脸图像?
- java - ActiveMQ 集群可以让代理运行在不同的传输协议上吗?