flutter / flutter/devtools

Expose ability to pump() and pumpAndSettle() from IDE while debugging widget tests

Đang mở
#2,150 18 bình luận 5 reaction 0 người được giao Xem trên GitHub
P3 screen: debugger
Ngôn ngữ chính
Dart
Star
1.7k
Fork
404
Merge trung bình
6 ngày 17 giờ
Pull request đã merge (30 ngày)
18

Mô tả

`pump([Duration])` and `pumpAndSettle()` are poorly understood APIs in Flutter widget testing. Most often, when a test unexpectedly fails, the users simple add pumps until it passes.

One such example we had in Greentea goes like this. Imagine the following API for model layer:

```dart
// Sub methods
sub();
unsub();

// Returns null if result is not cached and initiates a "fill" call.
// Returns result if it was cached.
ModelResult getResult({params});

// You then use this in a stateful widget:

initState() => sub();
dispose() => unsub();

Widget build(BuildContext context) {
final result = getResult(params);
if (result == null) {
// Render loading indicator
} else {
// Render UI.
}
}
```

This is pretty straight forward in a normal execution flow. When it came to testing, we replaced all model services such that the fill call would immediate return a mock result. That however, required an extra microtask to execute because the initial result would always be null until the fill method got called.

Because real code is not as simple as above and some of the logic was buried under different abstractions, it took us a while to realize that an extra pump() was needed in order to get the actual widget rendered.

There are many cases like this where the widget author might not even be aware of what's going in the model layer and how many pumps are needed.

One idea that might help is to provide an interactive pump() button. The pump button would be similar to "Step" button in a debugger except it would just flush microtasks and schedule a frame.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.