我正在使用Mocha,Mongoose和MongoDB。
我的目标只是围绕创建帐户进行测试。我有以下代码:
require( "./../../config/config" );
var mongoose = require( "mongoose" );
mongoose.connect( process.env.MONGODB_URI );
const expect = require( "expect" );
var { Account } = require( "./../../models/account" );
describe( "Account Creation", () =>
{
it( "should successfully create an account", ( done ) =>
{
var data =
{
username: "PrestonJ",
email: "someEmail@mail.com",
password: "somePassword123!"
};
var account = new Account( data );
account.save().then( ( result ) =>
{
console.log( "(temporary log) account saved" );
done();
}).catch( ( e ) => done( e ) );
});
});
Mongoose承诺链执行并将新帐户保存到数据库,但是从未达到done()
(即使已写入,并且上面的控制台调用也可以工作)。
这导致Mocha测试失败,并给了我:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我尝试仅return
的Promise链,但这也不起作用。我还尝试过将done
一起全部删除,但没有成功。
已经几个小时了,我一直找不到正确的做法/解决方法。
编辑: 我尝试增加Mocha的超时时间。错误是否被吞没了?我的模型文件中有东西吗?
编辑2: 这是帐户模型的来源:
var mongoose = require( "mongoose" );
const _ = require( "lodash" );
const validator = require( "validator" );
const jwt = require( "jsonwebtoken" );
const bcrypt = require( "bcryptjs" );
let Account_CollectionName = "accounts";
var Account_Schema = new mongoose.Schema(
{
username:
{
type: String,
minLength: 3,
trim: true,
required: true,
unique: true
},
email:
{
type: String,
minlength: 1,
trim: true,
required: false,
unique: true,
sparse: true,
validate: validator.isEmail,
message: "{VALUE} is not a valid email"
},
password:
{
type: String,
required: true,
minLength: 5
},
tokens:
[{
access:
{
type: String,
required: true
},
token:
{
type: String,
required: true
}
}],
},
{
collection: Account_CollectionName
});
Account_Schema.pre( "save", function( next )
{
var account = this;
if( account.isModified( "password" ) )
{
bcrypt.genSalt( 10, ( err, salt ) =>
{
bcrypt.hash( account.password, salt, ( err, hash ) =>
{
account.password = hash;
next();
});
});
}
else
next();
});
Account_Schema.methods.toJSON = function()
{
var account = this;
var accountObject = account.toObject();
return _.pick( accountObject,
[
"_id", "username", "email"
] );
};
Account_Schema.methods.generateAuthToken = function()
{
var account = this;
var access = "auth";
var token = jwt.sign( { _id: account._id.toHexString(), access }, process.env.JWT_SECRET ).toString();
account.tokens = account.tokens.concat( [{ access, token }] );
return account.save().then( () =>
{
return token;
});
};
Account_Schema.statics.findByToken = function( token )
{
var Account = this;
var decoded;
try {
decoded = jwt.verify( token, process.env.JWT_SECRET );
}
catch( e )
{
return Promise.reject();
}
return Account.findOne(
{
"_id" : decoded._id,
"tokens.token": token,
"tokens.access": "auth"
});
};
Account_Schema.statics.findByCredentials = function( username, password )
{
var Account = this;
return Account.findOne( { username } ).then( ( account ) =>
{
if( ! account )
return Promise.reject();
return new Promise( ( resolve, reject ) =>
{
bcrypt.compare( password, account.password, ( err, res ) =>
{
if( res )
resolve( account );
else
reject();
});
});
});
};
Account_Schema.methods.removeToken = function( token )
{
var account = this;
return account.update(
{
$pull:
{
tokens: { token }
}
});
};
var Account = mongoose.model( Account_CollectionName, Account_Schema );
module.exports = { Account };
答案 0 :(得分:1)
我知道了。需要完成许多事情;这是it
块的固定来源:
it( "should successfully create an account", () =>
{
var data =
{
username: "PrestonJ",
email: "someEmail@mail.com",
password: "somePassword123!"
};
let account = new Account( data );
return account.save().then( ( result ) =>
{
if( result )
{
console.log( "Successful!" );
return Promise.resolve();
}
else
return Promise.reject( "cannot save" );
}).catch( ( e ) =>
{
return Promise.reject( e );
});
});
done
/ done()
的痕迹return
account.save().then(...)...
then()
中,使用return Promise.resolve()
代替done()
catch()
中,使用return Promise.reject(e)
代替done(e)
答案 1 :(得分:0)
您收到此错误消息是因为mocha在保存操作完成之前等待了200ms的时间,并且由于此操作花费了超过200ms的时间,因此您的测试失败了。
处理此类问题的最佳方法是知道您的应用将花费最多的时间进行响应并将该时间设置为超时。
您可以通过添加标记timeout
您可以在mocha.opts文件中将其提供为
--timeout 50000
这将使您的测试用例等待50秒,然后超时才能进行任何操作。