I have a simple Dart class that is using the package Dio in one of its methods. In the class’s corresponding test file, I’m trying to mock Dio by placing @GenerateMocks([Dio])
above the main method. I then ran dart run build_runner build
to generate the mocks for Dio. But no mocks were generated. The command line outputted the following:
[INFO] Generating build script completed, took 143ms
[INFO] Reading cached asset graph completed, took 53ms
[INFO] Checking for updates since last build completed, took 628ms
[INFO] Running build completed, took 3ms
[INFO] Caching finalized dependency graph completed, took 41ms
[INFO] Succeeded after 48ms with 0 outputs (0 actions)
Why is it not generating anything?
I tried updating my dependencies thinking that maybe this was the issue but it wasn’t.
test: ^1.24.9
mockito: ^5.4.4
build_runner: ^2.4.8
I’ve run dart run build_runner build
from the root of the project, and I get 0 outputs. I’ve also run it from the folder where the test is located and that errored out.
I’ve also tried generating mocks for a different class, but that had the exact same result.
Any ideas for what build_runner isn’t generating anything?
2
Answers
I found the issue. It was because I had my tests in the
tests/
directory instead of thetest/
directory (no "s"). The code generator won't find the tests if they aren't in the test/ directory. 🤦🏼♂️ Once I put them in the correct dir, the code generator generated the mocks properly.In every file that has some code that needs to be generated, you have to include
part 'the_file_name.g.dart';
after the imports section. For example, if the file name isfoo.dart
, then you have to add this:This will tell build_runner to generate the missing part file.