Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Error when run very_good test #320

Closed
aldychris opened this issue Mar 18, 2022 · 1 comment · Fixed by #329
Closed

fix: Error when run very_good test #320

aldychris opened this issue Mar 18, 2022 · 1 comment · Fixed by #329
Assignees
Labels
bug Something isn't working as expected

Comments

@aldychris
Copy link

Description
No error when run flutter test but It's return this exception when Im try run test with very_good cli,

aldychris@Aldys-MBP my_flutter % very_good test
Running "flutter test" in /Users/aldychris/DevArea/Flutter/Workspace/my_flutter ✓ Optimizing tests (1.7s)

00:08 +153: ...rentUpgradablePlans passes, emit success state with isProceedEnable value from DFUnhandled exception:
RangeError (end): Invalid value: Not in inclusive range 154..234: 235
#0      RangeError.checkValidRange (dart:core/errors.dart:355:9)
#1      _StringBase.substring (dart:core-patch/string_patch.dart:400:27)
#2      _extension#4.truncated (package:very_good_cli/src/cli/flutter_cli.dart:386:30)
#3      _flutterTest.<anonymous closure> (package:very_good_cli/src/cli/flutter_cli.dart:304:36)
#4      _RootZone.runUnaryGuarded (dart:async/zone.dart:1618:10)
#5      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#6      _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#7      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
#8      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7)
#9      _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#10     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#11     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:122:13)
#12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:193:5)

We are using mockito: 5.0.17 on the test case

Steps To Reproduce

  1. Run very_good test on root project

Expected Behavior
No error

Additional Context
Add any other context about the problem here.

@aldychris aldychris added the bug Something isn't working as expected label Mar 18, 2022
@felangel felangel self-assigned this Mar 18, 2022
@Giuspepe
Copy link
Contributor

Giuspepe commented Mar 18, 2022

Hello :)
I'm having problems with very_good test as well. I have found a solution for the issue I'm having, maybe it's the same issue that you encountered.

I have two test files where each test file does some preparation work in setUp. The setUp of foo_test.dart should only be run for foo_test.dart. However, currently the setUp for foo_test.dart is also run for every other test because it is not wrapped in a group. All setUp bodies declared in all test files are ran for every single test instead of running the setUp body only for the test file declaring that body. Because of this, you may run into exceptions if your setUp bodies interfere with each other. That's why some tests fail when run with very_good test while they succeed when run with flutter test.

Minimum reproducible example

foo_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';

void main() {
  setUp(() {
    GetIt.instance.registerFactory<int>(() => 42);
  });

  tearDown(GetIt.instance.reset);

  test('foo', () {
    expect(GetIt.instance<int>(), equals(42));
  });
}

bar_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';

void main() {
  setUp(() {
    GetIt.instance.registerFactory<int>(() => 123);
  });

  tearDown(GetIt.instance.reset);

  test('bar', () {
    expect(GetIt.instance<int>(), equals(123));
  });
}

Generated .test_runner.dart

// GENERATED CODE - DO NOT MODIFY BY HAND
// Consider adding this file to your .gitignore.

import 'foo_test.dart' as foo_test_dart;
import 'bar_test.dart' as bar_test_dart;

void main() {
  foo_test_dart.main();
  bar_test_dart.main();
}

The tests fail with the exception Invalid argument(s): Object/factory with type int is already registered inside GetIt. because both setUp bodies are called for each test instead of only the test declaring setUp itself.

I suggest the following solution: wrap each test in a group:

Generate the following .test_runner.dart where each test is wrapped in a group so each test's setUp/tearDown calls are run only for the test declaring them itself and not for every other test as well.

// GENERATED CODE - DO NOT MODIFY BY HAND
// Consider adding this file to your .gitignore.

import 'package:flutter_test/flutter_test.dart';
import 'foo_test.dart' as foo_test_dart;
import 'bar_test.dart' as bar_test_dart;

void main() {
  group('foo_test_dart', foo_test_dart.main);
  group('bar_test_dart', bar_test_dart.main);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants