Repository navigation
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. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
First I created a project:
Then added a view:
Then added a service:
After that I modified
FeedViewModelto 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
HttpApiServiceclass:And in the
app.dartfile, changed the following line:To the following:
And also updated the
FeedViewModelclass to locate theApiServiceinstead ofHttpApiService:And also ran the
stacked generatecommand 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.dartfile and executedstacked generate:And the test passed ✅
Did I modify the correct files? Is there something I should have done differently?
All reactions