0

child-class component(BusinessRegistration)

export class BusinessRegistration extends BizAbstractRegistration {

constructor(protected readonly injector: Injector) {
super(injector);
}
other methods....
}

base class abstract with DI

export abstract class BizAbstractComponent implement BizActionHandler {

protected logger : NGXLogger;
protected anchorservice : AnchorService;

.....other services 

 protected constructor (protected readonly injector:Injector) {

 this.logger = injector.get(NGXLogger);
  this.anchorservice = injector.get(AnchorService);

 .... so on 

 }

}

child class test case

describe('BusinessRegistration', () => {
        Testbed.configureTestingModule({
            declarations: [BusinessRegistration],
            imports: [BusinessRegistrationModule],
            providers: [{
                    provide: NGXLogger,
                    useValue: jasmine.createSpyObj('logger', ['debug']),
                },
                {
                    provide: AnchorService,
                    useValue: jasmine.createSpyObj('anchorService', ['scrollToTop']),
                },
            ]
        }).compileComponents().then(() => {
            fixture = TestBed.inject < BusinessRegistration > (BusinessRegistration);
            component = fixture.componentInsatance;
            fixture.detectChanges();
        })

        describe('test for BusinessRegistration to be created', () => {
            it('expect BusinessRegistration to be truthy', () = {
                expect(component).toBeTruthy();
            });

        });

NullInjectionError: R3InjectorError(DynamicTestModule) [StoreFeatureModule ->ReducerManager->ReducerManager]

The test cases are failing with the above error.

I tried to create a concrete class and extend the abstract class and then mock the dependencies in that class and the instantiate that class too. but did not work .

Basically , I have a parent Abstract class for my component and it has dependencies injected in its constructor using 'injector.get()' . I want to write the unit test for my component.

raunaky50
  • 1
  • 1
  • Welcome to SO! [Here's how to ask a proper "Where's the bug / Fix my code" question](https://meta.stackoverflow.com/a/253788/11107541). Can you please read it and apply what you learn to improve your question? You can also read [ask] for further guidance. – starball Jan 03 '23 at 22:51

0 Answers0