【发布时间】:2019-08-19 22:54:56
【问题描述】:
我正在尝试使用 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 没有被 mock 替换。
【问题讨论】:
-
我在下面给出了一个建议答案,但这有点远,因为我没有`final SerializableFinder loginContinuePasswordButton = find.byValueKey('login_continue_password_button');`的代码。问题出在那儿的可能性很小。
-
嗨 Felipe,你能找到一种方法来模拟 Flutter 集成测试中的依赖关系吗?使用
DataHandler提到的here 怎么样?
标签: dart flutter mocking mockito flutter-test