Unit Testing Http status code

Issue

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

Solution

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

Answered By – Günter Zöchbauer

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *