stacked icon indicating copy to clipboard operation
stacked copied to clipboard

[bug]: When I delete a service it fails to delete tests correctly.

Open MDSADABWASIM opened this issue 2 years ago • 4 comments

Describe the bug

Suppose I created a service using this command: stacked create service realtime

Now I want to delete this using this command: stacked delete service realtime

It deletes most of the things but fails to delete the test with this response:

[SEVERE] mockito:mockBuilder on test/helpers/test_helpers.dart:
Invalid @GenerateMocks annotation: Mockito cannot mock unknown type `RealtimeService`. Did you misspell it or forget to add a dependency on it?
[INFO] Running build completed, took 10.5s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 274ms
[SEVERE] Failed after 10.7s
Command complete. ExitCode: 1

I tried it multiple times but it gives me the same behaviour.

To reproduce

Create a service: stacked create service realtime

delete the created service: stacked delete service realtime

Stacked CLI version: 1.11.2

Expected behavior

It deletes all the test-related code correctly.

Screenshots

No response

Additional Context

No response

MDSADABWASIM avatar Sep 19 '23 11:09 MDSADABWASIM

Hi @MDSADABWASIM , can you show me your test_helpers.dart file please?

ferrarafer avatar Sep 27 '23 03:09 ferrarafer

@ferrarafer After adding the realtime service(test_helpers.dart):

import 'package:camio_truck_owner/app/app.locator.dart';
import 'package:mockito/annotations.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:camio_truck_owner/services/local_storage_service.dart';
import 'package:camio_truck_owner/services/realtime_service.dart';
// @stacked-import

import 'test_helpers.mocks.dart';

@GenerateMocks([], customMocks: [
  MockSpec<NavigationService>(onMissingStub: OnMissingStub.returnDefault),
  MockSpec<LocalStorageService>(onMissingStub: OnMissingStub.returnDefault),
  MockSpec<RealtimeService>(onMissingStub: OnMissingStub.returnDefault),
// @stacked-mock-spec
])
void registerServices() {
  getAndRegisterNavigationService();
  getAndRegisterLocalStorageService();
  getAndRegisterRealtimeService();
// @stacked-mock-register
}

MockNavigationService getAndRegisterNavigationService() {
  _removeRegistrationIfExists<NavigationService>();
  final service = MockNavigationService();
  locator.registerSingleton<NavigationService>(service);
  return service;
}

MockLocalStorageService getAndRegisterLocalStorageService() {
  _removeRegistrationIfExists<LocalStorageService>();
  final service = MockLocalStorageService();
  locator.registerSingleton<LocalStorageService>(service);
  return service;
}

MockRealtimeService getAndRegisterRealtimeService() {
  _removeRegistrationIfExists<RealtimeService>();
  final service = MockRealtimeService();
  locator.registerSingleton<RealtimeService>(service);
  return service;
}
// @stacked-mock-create

void _removeRegistrationIfExists<T extends Object>() {
  if (locator.isRegistered<T>()) {
    locator.unregister<T>();
  }
}

It stays the same after deleting the service, I think it's happening cause CLI is deleting test_helpers.mocks.dart before removing the service from here.

MDSADABWASIM avatar Sep 27 '23 06:09 MDSADABWASIM

Hi @MDSADABWASIM , I was able to reproduce the issue, I will fix it as soon as possible.

In the meantime, a workaround is to follow this steps on test_helpers.dart:

  • Remove MockSpec<RealtimeService>(onMissingStub: OnMissingStub.returnDefault), line
  • Remove getAndRegisterRealtimeService(); line
  • execute stacked generate

ferrarafer avatar Sep 29 '23 03:09 ferrarafer

Hello i accidently deleted the test file and now i cant run stacked create service what i can do Screenshot 2024-04-03 at 12 23 32

ZackBela avatar Apr 03 '24 11:04 ZackBela