繁体   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