mobx.dart icon indicating copy to clipboard operation
mobx.dart copied to clipboard

testWidgets with Store not working properly

Open juanmanjarres opened this issue 1 year ago • 2 comments

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); 
  });
}

juanmanjarres avatar Feb 05 '24 18:02 juanmanjarres

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

Poloten avatar Feb 08 '24 12:02 Poloten

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

juanmanjarres avatar Feb 14 '24 18:02 juanmanjarres

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! 

amondnet avatar Mar 26 '24 08:03 amondnet