mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.39k stars 310 forks source link

testWidgets with Store not working properly #985

Closed juanmanjarres closed 5 months ago

juanmanjarres commented 7 months ago

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); 
  });
}
Poloten commented 7 months ago

Are you sure there are no errors with String in Text() ? Text('${counterStore.count}',),

juanmanjarres commented 6 months ago

Yes sorry I didn't copy the example correctly. The problem is still there though

amondnet commented 5 months ago

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!