等待我的班级初始化(或如何等待未来完成)? [英] Waiting for my class to initialize (or how to wait for a Future to complete)?
问题描述
我有一个类,调用一个async(Future)函数来启动数据库实例,如下所示:
< pre class =lang-dart prettyprint-override>
class DataManager {
bool DbIsReady = false;
Db _db;
DataManager(){
init_mongo_db();
}
void init_mongo_db(){
print(Initializing MongoDB);
_db = new Db(mongodb://127.0.0.1/test);
_db.open()。then((_){
DbIsReady = true;
});
}
Future< List> attemptLogin(String username,String password){
users = _db.collection(users);
return // ...为了清晰起见,余下的代码
}
}
这在服务器端工作,因为当服务器首次启动时,数据库被初始化。在用户实际尝试登录时,数据库已初始化,一切正常。但是,当我尝试为此创建集成测试时,这会失败。当我创建类时,数据库尚未初始化,因此运行AttemptLogin例程失败。
DataManager db = new DataManager();
//这可能会失败,因为db还没有初始化。
db.AttemptLogin(test,test);
更糟的是,代码使用Dart DI框架,所以我实际上没有直接控制初始化DataManager类。这是类的实际设置:
setUp((){
app .addModule(new Module()
..bind(DataManager)
app.setUp();
});
然后这是调用测试登录功能,最终调用attemptLogin函数失败:
var req = new MockRequest(/ user / login,contentType:'JSON',method:'POST',
body:JSON.encode
{username:'test',
password:'test'}));
如何处理数据库init的异步性质,并仍然进行模拟测试?具体来说,是否有一种方法强制attemptLogin()Future以某种方式等待DataManager类初始化完成?
感谢您的帮助,
Greg
使用@lrn的解决方案如何使用
setUp (){
return DataManager.createNew()。then((dm){
app.addModule(new Module()
..bind(DataManager,toValue:dm);
app.setUp();
}
});
这样你已经传递了一个初始化的 DataManager
实例进入DI。如果您以后请求,您总是可以确定它已经初始化。
Futures in Dart are the bane of my existence.
I have a class, which calls an async (Future) function to start up the database instance like so:
class DataManager {
bool DbIsReady = false;
Db _db;
DataManager() {
init_mongo_db();
}
void init_mongo_db() {
print("Initializing MongoDB");
_db = new Db("mongodb://127.0.0.1/test");
_db.open().then((_) {
DbIsReady = true;
});
}
Future<List> attemptLogin(String username, String password) {
users = _db.collection("users");
return // ... rest of code cut out for clarity
}
}
This works OK server-side because when the server first starts up, the database is initialized. By the time a user actually attempts to log in, the database has been initialized and all is well. However, this fails miserably when I try to create an integration test for this. When I create the class, the database isn't yet initialized, so running the AttemptLogin routine fails.
DataManager db = new DataManager();
// This fails miserably, because db hasn't initialized yet.
db.AttemptLogin("test", "test");
Worse, the code uses the Dart DI framework, so I don't actually have direct control over the initialization of the DataManager class. This is the actual setup of the class:
setUp(() {
app.addModule(new Module()
..bind(DataManager)
app.setUp();
});
And then this is the call to test the login functionality, which ultimately calls the attemptLogin function which fails:
var req = new MockRequest("/user/login", contentType: 'JSON', method:'POST',
body:JSON.encode(
{"username" : 'test',
"password" : 'test' }));
How does one deal with the async nature of the the database init and still do mock testing? Specifically, is there a way to force the attemptLogin() Future to somehow wait for the completion of the DataManager class initialization?
Thanks for your help, Greg
what about using @lrn s solution like
setUp(() {
return DataManager.createNew().then((dm) {
app.addModule(new Module()
..bind(DataManager, toValue: dm);
app.setUp();
}
});
This way you already pass an initialized DataManager
instance into the DI. If you request it later on you always can be sure it is already initialized.
这篇关于等待我的班级初始化(或如何等待未来完成)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!