如何编写单元测试来返回作为 Future 一部分的响应的状态代码? 在陷入困境之前我已经做到了这一点
import 'package:http/http.dart' as http;
test( "test future", (){
Future<Response> future = http.get( "http://www.google.com");
expect( future, completion( equals( ( Response e)=>(e.statusCode ), 200)));
});
失败并显示消息
FAIL: test future Expected: <Closure: (Response) => dynamic>
Actual: <Instance of 'Response'>
然后我尝试了
solo_test( "test response status", (){
http.get( "http://www.google.com").then( expectAsync0((response)=>expect( response.statusCode,200)));
});
失败并显示消息
Test failed: Caught type '() => dynamic' is not a subtype of type '(dynamic) => dynamic' of 'f'.
最佳答案
似乎可以这样工作
void main(List<String> args) {
test( "test future", (){
test( "test future", (){
Future<http.Response> future = http.get( "http://www.google.com");
expect(future.then((http.Response e) => e.statusCode), completion(equals( 200 )));
});
});
}
关于dart - 单元测试 Http 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490349/