我正在用mocha和chai测试我的'用户'代码,但是我一直收到此错误。用于创建模型的index.js文件是自动构建的,并且运行良好。然后,我尝试使用Mocha添加测试,但无法找出原因。
我的测试文件夹:
测试/用户/用户dbtest:
const {expect}=require('chai')
const{usernameExists}=require('../../services/user/user_db')
describe('user db tests',()=>{
it('check whether username alreddy exists',async()=>{
const check=await usernameExists('')
expect(check).to.be.false
expect(check===null).to.be.false
expect(check===undefined).to.be.false
});
});
服务/用户/用户数据库:
const db=require('../../models/index.js')
async function usernameExists(username){
const user= await db.user.findOne({
where:{username}
});
if(user) return user
return false
}
module.exports={usernameExists}
models / index.js:
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require('../config/config');
const db = {};
console.log(config)
const sequelize=new Sequelize(config.db.database,config.db.username,config.db.password,{
dialect:'mysql',
host:config.db.host
})
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
//ASSOCIATIONS
db.book_belongs_to.belongsTo(db.user)
db.book_belongs_to.belongsTo(db.book)
db.book_bought_by.belongsTo(db.user)
db.book_bought_by.belongsTo(db.book)
sequelize
.authenticate()
.then(()=>{
console.log("connected");
})
.catch(err=>{
console.error(err);
});
module.exports = db;
models / user.js
'use strict';
module.exports=(sequelize,Datatypes)=>{
return sequelize.define('user',{
username:{
type:Datatypes.STRING,
allowNull:false
},
firstname:{
type:Datatypes.STRING,
allowNull:false
},
middlename:{
type:Datatypes.STRING,
},
lastname:{
type:Datatypes.STRING,
allowNull:false
},
email:{
type:Datatypes.STRING,
allowNull:false,
isEmail:true
},
password:{
type:Datatypes.STRING,
allowNull:false
}},{
timestamps:false,
freezeTableName:true
})
}
提前感谢...
您应该使用存根/模拟库,例如sinon.js来模拟db.user.findOne
方法。
例如
user_db.js
:
const db = require('./index.js');
async function usernameExists(username) {
const user = await db.user.findOne({
where: { username },
});
if (user) return user;
return false;
}
module.exports = { usernameExists };
index.js
:
// simulate sequelize db
const db = {
user: {
findOne() {},
},
};
module.exports = db;
因为我们正在测试服务层,所以模型层无关紧要(我们对模型进行存根和模拟)。因此,此示例中没有模型层。
user_db.test.js
:
const { expect } = require('chai');
const sinon = require('sinon');
const { usernameExists } = require('./user_db');
const db = require('./index.js');
describe('user db tests', () => {
afterEach(() => {
sinon.restore();
});
it('should return non-existed user', async () => {
sinon.stub(db.user, 'findOne').resolves(false);
const check = await usernameExists('');
expect(check).to.be.false;
expect(check === null).to.be.false;
expect(check === undefined).to.be.false;
});
it('should return existed user', async () => {
const fakeUser = { username: 'anna' };
sinon.stub(db.user, 'findOne').resolves(fakeUser);
const check = await usernameExists('');
expect(check).to.be.deep.equal({ username: 'anna' });
});
});
带有覆盖率报告的单元测试结果:
user db tests
✓ should return non-existed user
✓ should return existed user
2 passing (27ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 50 | 100 |
index.js | 100 | 100 | 0 | 100 |
user_db.js | 100 | 100 | 100 | 100 |
------------|---------|----------|---------|---------|-------------------
源代码:https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/61695519