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'.

1 Answer 1

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 )));
      });
   });
}   
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.