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

Salma
  • 1,211
  • 2
  • 16
  • 33

7 Answers7

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.

Rowan Gontier
  • 821
  • 9
  • 14
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......
}
vivek s
  • 91
  • 1
  • 3
  • You made my day. I was testing the same code from `resocoder` like yours, and this solution fixed it – Arash Jan 11 '21 at 18:43
4

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!

p0wl
  • 658
  • 5
  • 10
3

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

  • 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. – farkerhaiku Nov 21 '19 at 21:03
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.

Soernt
  • 338
  • 2
  • 10
0

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

test('',(){
    // expect()/assertion should be here
})
//not here
Ait Bri
  • 498
  • 6
  • 15
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);
  }
}