简体   繁体   English

Flutter Dart测试带有服务器回调的http

[英]Flutter dart tests with http with server callbacks

I am developing some tests for my dart app, but I have some problems with callbacks on button presses. 我正在为我的dart应用程序开发一些测试,但是按钮按下时的回调存在一些问题。

For instance, I have a button with a server request callback. 例如,我有一个带有服务器请求回调的按钮。 When I tap the button with the tester an async suspension is called. 当我用测试仪点击按钮时,将调用异步暂停。 I've seen some workarounds for this using mock requests, but I want to perform the actual request to the server. 我已经看到了一些使用模拟请求的解决方法,但是我想对服务器执行实际的请求。 Is there any solution for this. 有什么解决办法吗?

Expected result: The tester taps on the button. 预期结果:测试仪点击按钮。 The button is making the call to the server and then the testing is continued after the request arrived/ after the current state refreshes (any of those would be great). 该按钮正在向服务器发出呼叫,然后在请求到达之后/当前状态刷新之后继续测试(这些都很好)。

If this is not possible, do you have any other suggestions for software to perform this kind of tests? 如果无法做到这一点,那么您对软件进行此类测试还有其他建议吗? Maybe through Jenkins? 也许通过詹金斯?

Code for tapping button: 点击按钮的代码:

 testWidgets("Open Login Test", (WidgetTester tester) async{
 await tester.pumpWidget(
    new MaterialApp(
      home: new Material(
        child: new LoginScreen(),
      ),
    ));

expect(find.text("Next"), findsOneWidget);
expect(find.text("Login"), findsNothing);

Finder emailField = find.byKey(new Key('email'));
await tester.enterText(emailField, "vlad_duncea_31@yahoo.com");

var submitButton = find.byKey(new Key('login'));
expect(submitButton, findsOneWidget);

await tester.tap(submitButton);
expect(find.text("Next"), findsNothing);
expect(find.text("Login"), findsOneWidget);

});

You should probably separate both logics. 您可能应该将两种逻辑分开。

In the first place you create the test for the UI mocking the response from the server as you say above. 首先,您要为UI创建测试,以模仿服务器的响应,如上所述。 This lets you test that the ui flow is the correct depending on the server response. 这使您可以根据服务器响应来测试ui流是否正确。

Andrea Bizotto provides a good example in one of his medium posts . 安德里亚·比索托(Andrea Bizotto)在他的一篇中等职位中提供了一个很好的例子。

And later you can test the logic with the server in a separate test. 之后,您可以在单独的测试中使用服务器测试逻辑。 For example, something along these lines. 例如,遵循这些原则的东西。

test('currentUser', () async {
  final Firebase user = await auth.currentUser();
  expect(user, isNotNull);
  expect(user.isAnonymous, isTrue);
  expect(user.isEmailVerified, isFalse);
  .....
});

The example is taken from the firebase plugin tests . 该示例取自firebase插件测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM