Unit test for abstract classes in projects generated by the CLI #1016
Unanswered
meysammahfouzi
asked this question in
Q&A
Replies: 1 comment
-
Hi @meysammahfouzi should be like below. MockApiService getAndRegisterApiService() {
_removeRegistrationIfExists<ApiService>();
final service = MockApiService();
locator.registerSingleton<ApiService>(service);
return service;
} Locator will do the rest under the hood. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First I created a project:
Then added a view:
Then added a service:
After that I modified
FeedViewModel
to have a reference to the service:Then wrote a unit test for the viewmodel and it passed ✅ (
feed_viewmodel_test.dart
):Now I decided to abstract the api service. So I manually created the following file:
api_service.dart
:And changed the
HttpApiService
class:And in the
app.dart
file, changed the following line:To the following:
And also updated the
FeedViewModel
class to locate theApiService
instead ofHttpApiService
:And also ran the
stacked generate
command to make sure all of the files are correct.Then I ran the unit test again and it failed 🔴 with the following error:
Bad state: GetIt: Object/factory with type ApiService is not registered inside GetIt.
To fix this, I modified the
test_helpers.dart
file and executedstacked generate
:And the test passed ✅
Did I modify the correct files? Is there something I should have done differently?
Beta Was this translation helpful? Give feedback.
All reactions