[英]Flutter: http.client always returns null
I'm applying a unit test over a http request in a Flutter application using Mockito.我正在使用 Mockito 在 Flutter 应用程序中对 http 请求应用单元测试。
Code to be tested:要测试的代码:
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));
}
}
response is always null !!响应始终是 null !
when I'm changing the client.post to http.post it works and returns the expected result当我将 client.post 更改为 http.post 它工作并返回预期结果
I'm applying something very similar to this https://flutter.dev/docs/cookbook/testing/unit/mocking我正在应用与此https://flutter.dev/docs/cookbook/testing/unit/mocking非常相似的东西
I doubted that the issue due to the localhost.我怀疑由于本地主机的问题。 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.
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.
Why I'm getting the null response?为什么我收到 null 响应? why the client.post is not working?
为什么client.post 不工作?
UPDATE更新
I noticed that the issue is not from the above code, it seems from the testing part.我注意到问题不是来自上面的代码,似乎来自测试部分。
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 version is ^5.0.5 Mockito 版本为^5.0.5
update this:更新这个:
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));
}
}
That problem was due the Mockito version, I was applying an old approach.这个问题是由于 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. 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.
using the @GenerateMocks([http.Client]) annotation before the main function is important
在主要 function 之前使用 @GenerateMocks([http.Client]) 注释很重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.