help-me-mom / ng-mocks

Angular testing library for mocking components, directives, pipes, services and facilitating TestBed setup
https://www.npmjs.com/package/ng-mocks
MIT License
1.04k stars 76 forks source link

Bug: investigate the issue #7495

Open satanTime opened 10 months ago

satanTime commented 10 months ago

https://stackoverflow.com/questions/77329862/ngmocks-error-ng0204-cant-resolve-all-parameters-for-pinresolver

ngMocks .... Error: NG0204: Can't resolve all parameters for PinResolver: (?, ?)

i am getting error within test... i use ngMocks... i used test from this documentation as template: https://ng-mocks.sudo.eu/guides/routing-resolver only difference that i can see is that i am using standalone components ... so there is no target module as in example within documentation pls? how should i mock parameters of this resolver

resolver:

export const PinResolver: ResolveFn<string> = (_, routerState: RouterStateSnapshot): Observable<string> => {
  const router = inject(Router);
  const toastService = inject(ToastService);
  const cardsService = inject(CardsService);
  const crypto = inject(CryptoService);
  const encOneTimeKey = crypto.encOneTimeKey;

  return crypto.setRsaKey(environment.danubepayRsaUrl.epin, 'pin').pipe(
    catchError(() => handleError(router, toastService)),
    switchMap(() =>
      cardsService
        .getPin(getCardId(routerState), crypto.pkFingerPrintKey('pin'), crypto.encryptWithRSA(encOneTimeKey, 'pin'))
        .pipe(
          map((res) => crypto.decryptTDES(res, encOneTimeKey)),
          concatMap((pin) => (pin === '' ? throwError(() => new Error('Cant decode the pinBlock')) : of(pin))),
          catchError(() => handleError(router, toastService)),
        ),
    ),
  );
};

test:

describe('PinResolver:fn', () =\> {
beforeEach(() =\> {
return MockBuilder(\[ActivatedRoute, RouterModule, RouterTestingModule.withRoutes(\[\]), NG_MOCKS_ROOT_PROVIDERS\])
.exclude(NG_MOCKS_GUARDS)
.exclude(NG_MOCKS_RESOLVERS)
.keep(PinResolver);
});

it('provides data to on the route', fakeAsync(() =\> {
const fixture = MockRender(RouterOutlet, {});
const router = ngMocks.get(Router);
const routerStateSnapshot = ngMocks.get(RouterStateSnapshot);
const location = ngMocks.get(Location);
const cardsService = ngMocks.get(CardsService);
const crypto = ngMocks.get(CryptoService);
const type = 'pin';

    cardsService.getPin = () => from(['1111']);

    location.pathname = '/pin';

    if (fixture.ngZone) {
      fixture.ngZone.run(() => router.initialNavigation());
      tick();
    }

    expect(location.pathname).toEqual('/cards/103867599/pin');

    const el = ngMocks.find(PinComponent);
    const route = ngMocks.findInstance(el, ActivatedRoute);

    expect(route.snapshot.data).toEqual({
      pin: {
        pinBlock: '1234',
      },
    });

}));
});

i try to follow ngMocks documentation but without success

Morad-m11 commented 10 months ago

@satanTime The question seems to have been removed

satanTime commented 9 months ago

Hi @Morad-m11, thanks not noting! I've copy-pasted the original description.