2

How can I write a unit test to return the status code of a response that is part of a Future? I got this far before getting stuck

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

This fails with the message

FAIL: test future   Expected: <Closure: (Response) => dynamic>
    Actual: <Instance of 'Response'>

I then tried

 solo_test( "test response status", (){
    http.get( "http://www.google.com").then( expectAsync0((response)=>expect( response.statusCode,200)));
  });

Which fails with the message

Test failed: Caught type '() => dynamic' is not a subtype of type '(dynamic) => dynamic' of 'f'.
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
richard
  • 2,887
  • 5
  • 26
  • 36

1 Answers1

2

Seems to work this way

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 )));
      });
   });
}   
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • thanks, I guess i am struggling to get my head around the fact that a the 'then' method on a Future is also a Future. – richard Dec 10 '13 at 20:52