簡體   English   中英

Flutter:http.client 總是返回 null

[英]Flutter: http.client always returns null

我正在使用 Mockito 在 Flutter 應用程序中對 http 請求應用單元測試。

要測試的代碼:

abstract class LoginRemoteDataSource {
  Future<LoginModel> login(String email, String password);
}

class LoginRemoteDataSourceImplementation implements LoginRemoteDataSource {
  http.Client client;

  LoginRemoteDataSourceImplementation({@required this.client});

  @override
  Future<LoginModel> login(String email, String password) async {
    // httpClient
    final response = await client.post(
        Uri.http("localhost:80", "/api/v1/auth/login"),
        headers: <String, String>{
          'Content-type': 'application/json; charset=UTF-8'
        },
        body:
            jsonEncode(<String, String>{'email': email, 'password': password}));
    print(response);
    return LoginModel.fromJson(json.decode(response.body));
  }
}

響應始終是 null !

當我將 client.post 更改為 http.post 它工作並返回預期結果

我正在應用與此https://flutter.dev/docs/cookbook/testing/unit/mocking非常相似的東西

我懷疑由於本地主機的問題。 but I tried a simple get request over a placeholder api for a testing response purposes https://jsonplaceholder.typicode.com and also it returns null.

為什么我收到 null 響應? 為什么client.post 不工作?

更新

我注意到問題不是來自上面的代碼,似乎來自測試部分。

class MockHttpClient extends Mock implements http.Client {}
void main() {
  LoginRemoteDataSourceImplementation loginRemoteDataSourceImplementation;
  LoginModel testingLoginModel;
  MockHttpClient mockHttpClient;


  setUp(() {
    mockHttpClient = MockHttpClient();
    loginRemoteDataSourceImplementation =
        LoginRemoteDataSourceImplementation(client: mockHttpClient);
testingLoginModel =
    LoginModel.fromJson(json.decode(fixture('login_fixture.json')));
  });


test("Should return login model when the request code is 200", () async {
  // arrange
  when(mockHttpClient.post(any, headers: anyNamed('headers'))).thenAnswer(
      (_) async => http.Response(fixture('login_fixture.json'), 200));
  // act
  final result = await loginRemoteDataSourceImplementation.login(
      'testingEmail', 'testingPassword');

  // assert
  expect(result, equals(testingLoginModel));
}, tags: 'login');
}

Mockito 版本為^5.0.5

更新這個:

abstract class LoginRemoteDataSource {
  Future<LoginModel> login(String email, String password);
}

class LoginRemoteDataSourceImplementation implements LoginRemoteDataSource {
  http.Client client = http.Client(); //<-- update

  LoginRemoteDataSourceImplementation(); //<-- update

  @override
  Future<LoginModel> login(String email, String password) async {
    // httpClient
    final response = await client.post(
        Uri.http("localhost:80", "/api/v1/auth/login"),
        headers: <String, String>{
          'Content-type': 'application/json; charset=UTF-8'
        },
        body:
            jsonEncode(<String, String>{'email': email, 'password': password}));
    print(response);
    return LoginModel.fromJson(json.decode(response.body));
  }
}

這個問題是由於 Mockito 版本造成的,我采用的是舊方法。

Then new one is exactly the as the https://flutter.dev/docs/cookbook/testing/unit/mocking by generating a new mock file thats creates a mock class extends the mock and implement the http.client using the builder_runner package.

在主要 function 之前使用 @GenerateMocks([http.Client]) 注釋很重要

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM