首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤振集成测试中模拟http请求?

如何在颤振集成测试中模拟http请求?
EN

Stack Overflow用户
提问于 2019-03-29 03:13:53
回答 3查看 13.3K关注 0票数 11

我试着用Mockito做这件事,这是我的测试:

代码语言:javascript
复制
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请求代码:

代码语言:javascript
复制
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;
}

这里我有一个全球性的:

代码语言:javascript
复制
library utgard.globals;

import 'package:http/http.dart' as http;

http.Client httpClient = http.Client();

但是,我继续收到http错误,这表明http没有被模拟替换。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-05-30 04:32:59

我找到的解决方案是在test_driver/app.dart中定义模拟,然后调用runApp函数:

代码语言:javascript
复制
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());
}
票数 3
EN

Stack Overflow用户

发布于 2019-03-29 08:29:39

而不是

代码语言:javascript
复制
      when(client.post('http://wwww.google.com'))
          .thenAnswer((_) async => http.Response('{"title": "Test"}', 200));

尝试any,然后稍后断言它

代码语言:javascript
复制
        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更安全。

票数 2
EN

Stack Overflow用户

发布于 2022-09-17 20:10:45

我们没有看到您正在测试的代码,但是它不太可能提出这个请求:

代码语言:javascript
复制
client.post('http://wwww.google.com')

无论如何,使用模拟json文件是一种很好的做法,而且您不希望在这些模拟文件更改时更改请求。

我建议您使用乳尾而不是Mockito。

这样,您就可以用any模拟任何调用:

代码语言:javascript
复制
// Simulate any GET :
mockHttpClient.get(any()))

// Simulate any POST :
mockHttpClient.post(any()))

以下是完整的解决方案:

代码语言:javascript
复制
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

    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55409978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档