我试着用Mockito做这件事,这是我的测试:
import 'package:http/http.dart' as http;
import 'package:utgard/globals.dart' as globals;
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
class MockClient extends Mock implements http.Client {}
void main() {
group('Login flow', () {
final SerializableFinder loginContinuePasswordButton =
find.byValueKey('login_continue_password_button');
FlutterDriver driver;
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
//await driver.close();
}
});
test('login with correct password', () async {
final client = MockClient();
when(client.post('http://wwww.google.com'))
.thenAnswer((_) async => http.Response('{"title": "Test"}', 200));
globals.httpClient = client;
await driver.enterText('000000');
await driver.tap(loginContinuePasswordButton);
});
});
}这是我的http请求代码:
Future<Map<String, dynamic>> post({
RequestType requestType,
Map<String, dynamic> body,
}) async {
final http.Response response =
await globals.httpClient.post('http://wwww.google.com');
print(response);
final Map<String, dynamic> finalResponse = buildResponse(response);
_managerErrors(finalResponse);
return finalResponse;
}这里我有一个全球性的:
library utgard.globals;
import 'package:http/http.dart' as http;
http.Client httpClient = http.Client();但是,我继续收到http错误,这表明http没有被模拟替换。
发布于 2019-05-30 04:32:59
我找到的解决方案是在test_driver/app.dart中定义模拟,然后调用runApp函数:
import 'package:flutter/widgets.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:utgard/business/config/globals.dart';
import 'package:utgard/main.dart' as app;
class MockClient extends Mock implements http.Client {}
void main() {
enableFlutterDriverExtension();
final MockClient client = MockClient();
// make your mocks here
httpClient = client;
runApp(app.MyApp());
}发布于 2019-03-29 08:29:39
而不是
when(client.post('http://wwww.google.com'))
.thenAnswer((_) async => http.Response('{"title": "Test"}', 200));尝试any,然后稍后断言它
when(
mockHttpClient.send(any),
).thenAnswer((_) async => http.Response('{"title": "Test"}', 200));
// ...
final String capt = verify(client.send(captureAny)).captured;
expect(capt, 'http://wwww.google.com');调用param不太可能是您所嘲笑的,所以使用any更安全。
发布于 2022-09-17 20:10:45
我们没有看到您正在测试的代码,但是它不太可能提出这个请求:
client.post('http://wwww.google.com')无论如何,使用模拟json文件是一种很好的做法,而且您不希望在这些模拟文件更改时更改请求。
我建议您使用乳尾而不是Mockito。
这样,您就可以用any模拟任何调用:
// Simulate any GET :
mockHttpClient.get(any()))
// Simulate any POST :
mockHttpClient.post(any()))以下是完整的解决方案:
class MockClient extends Mock implements http.Client {}
class FakeUri extends Fake implements Uri {}
void main() {
setUp(() {
registerFallbackValue(FakeUri()); // Required by Mocktail
});
tearDown(() {});
MockHttpClient mockHttpClient = MockHttpClient();
group('Login flow', () {
test('login with correct password', () async {
when(() => mockHttpClient.get(any())).thenAnswer(((_) async {
return Response(mockWeatherResponse, 200);
}));
// Call the functions you need to test here
}
}
}https://stackoverflow.com/questions/55409978
复制相似问题