【发布时间】:2019-08-20 19:35:40
【问题描述】:
我正在尝试使用 mockito 在颤振中模拟 httpRequest。
这里我定义了一个全局http客户端:
library utgard.globals;
import 'package:http/http.dart' as http;
http.Client httpClient = http.Client();
然后我在集成测试中替换:
import 'package:flutter_driver/driver_extension.dart';
import 'package:http/http.dart' as http;
import 'package:utgard/globals.dart' as globals;
import 'package:mockito/mockito.dart';
import 'package:utgard/main.dart' as app;
class MockClient extends Mock implements http.Client {}
void main() {
final MockClient client = MockClient();
globals.httpClient = client;
enableFlutterDriverExtension();
app.main();
}
然后我尝试使用 when 的 mockito:
test('login with correct password', () async {
final client = MockClient();
when(globals.httpClient.post('http://www.google.com'))
.thenAnswer((_) async => http.Response('{"title": "Test"}', 200));
await driver.enterText('000000');
await driver.tap(loginContinuePasswordButton);
});
但我收到以下错误:
错误状态:未在
when()内调用模拟方法。是否调用了真正的方法?
【问题讨论】:
标签: dart flutter mockito flutter-test