testWidgets with Store not working properly
Hi, I was trying to implement some widget tests using testWidgets but I can't seem to be able to get those to work with observables. I'm wondering if there's any issues with that specifically? The Card never updates, it always stays with the same value of 0 even though I call increment(). I've also tried to use the tester.runAsync() and waiting for an update but it doesn't work.
Here's some small code that shows the issue:
// counter_store.dart
import 'package:mobx/mobx.dart';
part 'counter_store.g.dart';
class CounterStore = _CounterStore with _$CounterStore;
abstract class _CounterStore with Store {
@observable
int count = 0;
@action
void increment() {
count++;
}
}
// card_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import '../counter_store.dart';
import 'package:provider/provider.dart';
void main() {
testWidgets('Card with incrementing count', (WidgetTester tester) async {
final counterStore = CounterStore();
await tester.pumpWidget(
Card(
child: Observer(
builder: (_) => Text(
'${counterStore.count}',
),
),
),
);
counterStore.increment();
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
}
Are you sure there are no errors with String in Text() ? Text('${counterStore.count}',),
Yes sorry I didn't copy the example correctly. The problem is still there though
There is nothing wrong with this behavior, I would appreciate it if you could provide a reproducible source code repository.
// card_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_test/flutter_test.dart';
import 'counter_store.dart';
void main() {
testWidgets('Card with incrementing count', (WidgetTester tester) async {
final counterStore = CounterStore();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Observer(
builder: (_) => Text(
'${counterStore.count}',
),
),
),
);
counterStore.increment();
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
}
flutter test test
00:02 +1: All tests passed!