单元测试Http状态码

时间:2013-12-10 09:17:35

标签: dart dart-async dart-unittest

如何编写单元测试以返回属于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'.

1 个答案:

答案 0 :(得分:1)

似乎以这种方式工作

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 )));
      });
   });
}