【问题标题】:Unit Testing Http status code单元测试 Http 状态码
【发布时间】:2013-12-10 09:17:35
【问题描述】:

如何编写单元测试来返回作为 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'.

【问题讨论】:

    标签: dart dart-async dart-unittest


    【解决方案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 )));
          });
       });
    }   
    

    【讨论】:

    • 谢谢,我想我很难理解这样一个事实,即 Future 上的“then”方法也是 Future。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2016-08-18
    • 1970-01-01
    • 2011-08-25
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多