flutter test fails. compiler error getter not found 'main'
Asked Answered
M

7

11

I'm trying to run flutter test using the terminal and i receive the following error. the following file C:/Users/User/AppData/Local/Temp/flutter_test_listener.e6fa92b4-6cd1-11e9-b9cb-68f728ca4610/listener.dart doesn't exist in the directory specified.

here's the error:

Compiler message: file:///C:/Users/User-45/AppData/Local/Temp/flutter_test_listener.e6fa92b4-6cd1-11e9-b9cb-68f728ca4610/listener.dart:46:17:

Error:Getter not found: 'main'. return test.main;

my project's directory is in another folder. how can i solve this? Thank you

Monteiro answered 2/5, 2019 at 12:12 Comment(0)
S
14

In my case, it happened that one of my helper classes in the test folder, had a name that ended "_test", and the compiler was looking for a main method. So I renamed the helper class, and problem solved.

Stack answered 1/7, 2020 at 0:11 Comment(2)
I had to execute flutter clean after I renamed the helper classes and then it works fine :)Preparatory
OMG it really saved me... I was clueless on this errorMohamed
T
7

It seems this error can have a lot of different causes. In my case the problem was a file foo_test.dart with the only content //TODO. So it was basically empty and the compiler could not find the excpected main method. The weird thing was, that it threw the Error in another test!

When I removed foo_test.dart, it worked again! Hope this helps!

Tressa answered 9/6, 2020 at 12:37 Comment(0)
M
6

If anybody faces the above problem then try to add main() in test class and if you have added then remove it out of test class curly braces. I faced this and removed the main() out of test class.

Eg...

class GetConcreteNumberTrivia_Test extends Mock
    implements NumberTriviaRepository {}

void main() {

.....my implementation......
}
Measles answered 13/5, 2020 at 15:44 Comment(1)
You made my day. I was testing the same code from resocoder like yours, and this solution fixed itSimonette
S
3

I had this error when a merge mess put the main() within a class.

Spinach answered 20/8, 2019 at 14:27 Comment(1)
I didn't have a merge mess, I just came from java and put the main method in a test class because I didn't know any better. This is likely the right answer.Oberhausen
O
1

I also got this error. My problem has been how I named the files for my tests. The test runner is looking for this pattern "*_test.dart" (hier). After renaming my test files accordingly, the test runner executes the test without any errors.

Overburdensome answered 18/10, 2019 at 5:38 Comment(0)
G
0

In my case, I had kept my expect() condition outside of the test method

test('',(){
    // expect()/assertion should be here
})
//not here
Greet answered 28/7, 2021 at 12:30 Comment(0)
C
0

This worked for the latest dependencies (01/16/2022). Complete answer for 2-3 errors:

added: build_runner: ^2.1.7 to pubspec.yaml

//...
import 'package:mockito/annotations.dart';

class MockNumberTriviaRepository extends Mock implements NumberTriviaRepo {}

@GenerateMocks([NumberTriviaRepo])
void main() {
  late GetConcreteNumberTrivia usecases;
  late MockNumberTriviaRepository mockNumberTriviaRepository;
  late int tNumber;
  late NumberTrivia tNumberTrivia;
  setUp(() {
    mockNumberTriviaRepository = MockNumberTriviaRepository();
    usecases = GetConcreteNumberTrivia(repo: mockNumberTriviaRepository);
    tNumber = 1;
    tNumberTrivia = NumberTrivia(text: 'test', number: 1);
  });

  test('should get a trivia for the number from the repo', () async {
    // arrange
    when(mockNumberTriviaRepository.getConcreteNumberTrivia(tNumber))
        .thenAnswer((_) async => Right(tNumberTrivia));
    // act
    final result = await usecases.execute(number: tNumber);
    //assert
    expect(result, Right(tNumberTrivia));
    verify(mockNumberTriviaRepository.getConcreteNumberTrivia(tNumber));
    verifyNoMoreInteractions(mockNumberTriviaRepository);
  });
}

class NumberTriviaRepository:

abstract class NumberTriviaRepository {
  Future<Either<Failure, NumberTrivia>>? getConcreteNumberTrivia(int number);
  Future<Either<Failure, NumberTrivia>>? getRandomNumberTrivia();
}

class GetConcreteNumberTrivia:

class GetConcreteNumberTrivia {
  final NumberTriviaRepository repo;
  GetConcreteNumberTrivia({
    required this.repo,
  });

  Future<Either<Failure, NumberTrivia>?> execute({required int number}) async {
    return await repo.getConcreteNumberTrivia(number);
  }
}
Caz answered 16/1, 2022 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.