Javascript 试图用sinon编写我的第一个mocha单元测试,但无法获得正确的心智模型
我昨天一整天都在做这件事,今天早上我试图通过在用户模型中伪造/模仿/存根mongoose调用,让心智模型正确地对userController进行适当的单元测试 我已经为userController编写了所有我认为需要的测试的脚手架,但是我正在努力让模拟工作起来 目前我得到:Javascript 试图用sinon编写我的第一个mocha单元测试,但无法获得正确的心智模型,javascript,node.js,mocha.js,chai,sinon,Javascript,Node.js,Mocha.js,Chai,Sinon,我昨天一整天都在做这件事,今天早上我试图通过在用户模型中伪造/模仿/存根mongoose调用,让心智模型正确地对userController进行适当的单元测试 我已经为userController编写了所有我认为需要的测试的脚手架,但是我正在努力让模拟工作起来 目前我得到: 1) userController getUserByID should return a user if id is valid & the user exists: Ty
1) userController
getUserByID
should return a user if id is valid & the user exists:
TypeError: Cannot read property 'have' of undefined
at Context.<anonymous> (test/controllers/userController.spec.js:58:25)
你的代码有几个错误
should
,您需要调用它。()getUserByID()
需要两个参数getUserByID()
返回承诺,无需将其赋值给变量并检查值Model.findById()
返回查询,您需要exec()
从查询中获取值。()userModel
创建存根方法findById
,您需要创建存根,而不是替换它const user=require('./userModel');
// 2. 此函数请求2个参数!
// 3. 此函数返回承诺。
const getUserByID=async(req,res)=>{
试一试{
//4.别忘了添加.exec()
// https://mongoosejs.com/docs/api.html#model_Model.findById
const foundUser=wait user.findById(req.params.id).exec();
const returnUser=JSON.parse(JSON.stringify(foundUser));
删除returnUser.password;
res.status(200).json({user:returnUser});
}捕捉(错误){
json({error:err.message});
}
};
module.exports={getUserByID};
文件userController.spec.js
//1。您只需要在expect、assert和should之间选择一个。
//对于这个例子,我们应该尝试。
// https://www.chaijs.com/guide/styles/#should
require('chai')。should();
const sinon=要求(“sinon”);
const userController=require('./userController');
const userModel=require('./userModel');
描述('userController',函数(){
描述('getUserByID',函数(){
它('如果id有效,则应返回用户,'用户存在',异步函数(){
//伪造用户文档。
常量伪造用户={
id:1,
密码:“userPassword”,
firstName:'userFirstName',
lastName:'userLastName',
电邮:'user@email.com',
过敏原:[“树”、“草”],
严重性:“高”,
createdAt:Date.now,
};
//5.创建存根userModel方法findById。
const stubUserFindById=sinon.stub(userModel,'findById');
stubUserFindById.returns({
执行官:sinon.fake.resolves(伪造用户),
});
//为res.status和res.status.json创建假函数。
const fakeResJson=sinon.fake();
const fakeResStatus=sinon.fake.returns({
json:fakeResJson,
});
//创建虚拟请求:满足getUserById输入(1)。
const req={params:{id:1}};
//创建虚拟响应。
常数res={
状态:fakeResStatus,
};
//2.函数getUserById需要2个参数!
//3.函数getUserById返回承诺,因此不需要检查结果。
等待userController.getUserByID(req,res);
//但是请验证是否使用正确的参数调用了stub&fake。
//验证调用的存根。
stubUserFindById.calledOnce.should.equal(true);
//验证使用正确参数调用的存根。
stubUserFindById.calledWith(req.params.id).should.equal(true);
//验证是否调用了伪res.status。
fakeResStatus.calledOnce.should.equal(真);
//验证使用参数200调用的伪res.status。
fakeResStatus.calledWith(200).should.equal(true);
//验证调用的伪res.status.json。
fakeResJson.calledOnce.should.equal(true);
//验证使用正确参数调用的伪res.status.json。
fakeResJson.args[0][0].should.be.an('object');
fakeResJson.args[0][0]。应为.have.property('user');
//验证已删除属性密码。
fakeResJson.args[0][0]。user.should.not.have.property('password');
fakeResJson.args[0][0].user.should.have.property('id',fakeUser.id);
fakeResJson.args[0][0].user.should.have.property('firstName',fakeUser.firstName);
fakeResJson.args[0][0].user.should.have.property('lastName',fakeUser.lastName);
//并检查其他属性。
//恢复存根。
stubUserFindById.restore();
});
});
});
用摩卡咖啡来运行它
$ mocha userController.spec.js
userController
getUserByID
✓ should return a user if id is valid & the user exists
1 passing (9ms)
$
希望这对您有所帮助。您的代码有几个错误
should
,您需要调用它。()getUserByID()
需要两个参数getUserByID()
返回承诺,无需将其赋值给变量并检查值Model.findById()
返回查询,您需要exec()
从查询中获取值。()userModel
创建存根方法findById
,您需要创建存根,而不是替换它const user=require('./userModel');
// 2. 此函数请求2个参数!
// 3. 此函数返回承诺。
const getUserByID=async(req,res)=>{
试一试{
//4.别忘了添加.exec()
// https://mongoosejs.com/docs/api.html#model_Model.findById
const foundUser=wait user.findById(req.params.id).exec();
const returnUser=JSON.parse(JSON.stringify(foundUser));
删除returnUser.password;
res.status(200).json({user:returnUser});
}捕捉(错误){
json({error:err.message});
}
};
module.exports={getUserByID};
文件userController.sp