After reviewing an example on how to display components based on a user's role at this link:
I'm encountering compilation issues due to missing arguments in the constructor within has-role.directive.spec.ts
. The constructor in has-role.directive.ts
requires 3 arguments: ViewContainerRef and TemplateRef from Angular core, along with a service for retrieving user roles.
has-role.directive.ts
constructor(
private viewContainerRef: ViewContainerRef,
private templateRef: TemplateRef<any>,
private authenticationService: AuthenticationService)
However, in the provided tutorial example, it is utilized as follows:
has-role.directive.spec.ts
describe('HasRoleDirective', () => {
it('should create an instance', () => {
const directive = new HasRoleDirective();
expect(directive).toBeTruthy();
});
});
Why does the example work without any complaints about missing arguments while I encounter issues?
UPDATE:
Following Michał's suggestion, I have created classes to include in the constructor:
class TestViewContainerRef extends ViewContainerRef {
element: ElementRef<any>;
injector: Injector;
parentInjector: Injector;
clear(): void {
throw new Error('Method not implemented.');
}
// more methods here...
}
let viewContainerRefMock = {
viewContainerRef: TestViewContainerRef
};
describe('HasRoleDirective', () => {
it('should create an instance', () => {
const directive = new HasRoleDirective(viewContainerRefMock, templateRefMock, authenticationServiceMock);
expect(directive).toBeTruthy();
});
});
Now, the issue shifts to my class itself: https://i.sstatic.net/11P7x.jpg