我试图创建一些测试Flutter应用程序与Mockito。
这是我的代码:
@GenerateMocks([http.Client])
main() {
group('sendUrlPost', () {
test('returns alias and links if the post call is successful', () async {
final client = MockClient();
String apiUrl = "someApiURL";
String longUrl = "someUrl";
when(client.post(Uri.parse(apiUrl),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(
{"url": longUrl}))).thenAnswer((_) async => http.Response(
'{"alias":"70492","_links":{"self":"https://www.youtube.com","short":"https://url-shortener-nu.herokuapp.com/short/70492"}}',
200));
expect(await UrlPostConnector.sendUrlPost(longUrl), isA<UrlResponse>());
});
});
}
我完全遵循了官方指南中的内容:https://docs.flutter.dev/cookbook/testing/unit/mocking,但当我创建模拟客户端时,我得到以下错误:
最后客户端= MockClient();
应为1个位置参数,但找到0个。请尝试添加缺少的参数
我猜指南已经过时了,但我还没有找到一种方法来使它工作。如果你能告诉我我做错了什么,如果有更好的方法来测试Http请求,我将不胜感激。
谢谢
2条答案
按热度按时间kupeojn61#
我缺少生成的mocks.dart文件,该文件是通过运行
感谢@jamesdlin指出这一点。
mnowg1ta2#
您必须将版本降低到
5.0.0
请确保在创建测试类后运行此命令