异步代码的UnitTest示例 [英] UnitTest example for asynchronous code
问题描述
在阅读 Dart单元测试之后,我会仍然不能理解如何使用未来
。
例如:
void main()
{
group('database group',(){
setUp((){
// Setup
});
tearDown
// TearDown
});
test('open connection to local database',(){
DatabaseBase database = null;
expect(database = new MongoDatabase(127.0.0.8,simplechat-db),isNotNull);
database.AddMessage(null).then(
expectAsync1(e)
{
// All ok
}
},
onError:(err)
{
expectAsync1(bb)
{
fail('error!');
}
}
);
});
//在这里添加更多测试
});
}
所以在测试中我创建一个基本抽象类的实例 DatabaseBase
实际的 MongoDb 类,并立即检查是否创建。然后我只是运行一些非常简单的函数: AddMessage
。此函数定义为:
Future AddMessage(String message);
并返回 completer.future
。
如果传递 message
为null,那么函数将失败completer: .completeError 'message'不能为null');
在实际测试中,我想测试 Future
已成功完成或出现错误。上面这是我尝试了解如何测试未来
返回 - 问题是这个测试不会失败:(
你能回答一个小代码示例如何测试返回 Future
的函数?在测试中我的意思是 - 有时候我想测试返回(成功)值和失败测试如果成功值不正确,并且另一个测试失败,则函数将失败未来
并输入 onError: / code> block。
我刚刚重新阅读了你的问题,错误的问题...
我相信你使用 expectAsync
不正确。 expectAsync
用于包装带有N个参数的回调,并确保它运行 count
次(默认为1)。
expectAsync
将确保任何异常被测试本身捕获并被返回,它本身并不实际运行任何期望(坏命名) p>
你想要的只是:
database.AddMessage(null).then(
(value){/ *不要做任何事情!没有期望=成功! * /},
onError:(err){
//这就足够了!
fail('error!');
}
);
或如果您需要确保测试完成某个特定值:
database.AddMessage(null).then(
expectAsync1((value){/ *检查这里如果你想要的话。* /}),
onError:(err){
//只要失败就足够了
fail('error!');
}
);
另一种方法是使用完成
匹配器。
注册一个异步期望,如果
// Future完成一个值,它将通过。
expect(database.AddMessage(null),completed);
或测试异常:
//同样,但这只会在未来完成异常时传递。
expect(database.AddMessage(null),throws);
如果要检查完成的值,您可以执行以下操作:
expect(database.AddMessage(null).then((value){
expect(value,isNotNull) ;
}),completed);
查看:
- http://api.dartlang.org/docs/bleeding_edge/matcher。 html#完成
- http ://api.dartlang.org/docs/bleeding_edge/unittest.html#expectAsync0
- http://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests
After reading the Unit Testing with Dart somehow I'm still can not understand how to use it with Future
s.
For example:
void main()
{
group('database group',(){
setUp( () {
// Setup
});
tearDown((){
// TearDown
});
test('open connection to local database', (){
DatabaseBase database = null;
expect(database = new MongoDatabase("127.0.0.8", "simplechat-db"), isNotNull);
database.AddMessage(null).then(
(e) {
expectAsync1(e)
{
// All ok
}
},
onError: (err)
{
expectAsync1(bb)
{
fail('error !');
}
}
);
});
// Add more tests here
}); }
So in the test I create an instance of base abstract class DatabaseBase
with some parameters to actual MongoDb class and immediately check if it created. Then I just run some very simple function: AddMessage
. This function defined as:
Future AddMessage(String message);
and return completer.future
.
If passed message
is null then the function will fail completer as: .completeError('Message can not be null');
In actual test I want to test if Future
completed successfully or with error. So above this is my try to understand how to test Future
returns - the problems is this this test does not fail :(
Could you write in answer a little code example how to test functions that return Future
? And in test I mean - sometimes I want to test return (on success) value and fail test if success value is incorrect and another test should fail then function will fail Future
and enter to onError:
block.
I just re-read your question, and I realize I was answering sort of the wrong question...
I believe you're using expectAsync
incorrectly. expectAsync
is used to wrap a callback with N parameters and ensure that it ran count
times (default 1).
expectAsync
will ensure that any exceptions are caught by the test itself and are returned. It does not actually run any expectations by itself (bad nomenclature.)
What you want is just:
database.AddMessage(null).then(
(value) { /* Don't do anything! No expectations = success! */ },
onError: (err) {
// It's enough to just fail!
fail('error !');
}
);
or if you need to ensure that the test completed to some particular value:
database.AddMessage(null).then(
expectAsync1((value) { /* Check the value in here if you want. */ }),
onError: (err) {
// It's enough to just fail!
fail('error !');
}
);
Another way of doing this is to use the completes
matcher.
// This will register an asynchronous expectation that will pass if the
// Future completes to a value.
expect(database.AddMessage(null), completes);
or to test for an exception:
// Likewise, but this will only pass if the Future completes to an exception.
expect(database.AddMessage(null), throws);
If you want to check the completed value, you could do as follows:
expect(database.AddMessage(null).then((value) {
expect(value, isNotNull);
}), completes);
See:
- http://api.dartlang.org/docs/bleeding_edge/matcher.html#completes
- http://api.dartlang.org/docs/bleeding_edge/unittest.html#expectAsync0
- http://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests
这篇关于异步代码的UnitTest示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!