我有以下指令,其中有一些自定义私有功能
@Directive({
selector: '[appAccessPermission]'
})
export class AccessPermissionDirective implements OnInit {
@Input() claims: string[];
@Input() shouldAllClaimsRequired = true;
@Input() shouldDisableThanHide = true;
constructor(private el: ElementRef, private authService: AuthenticationService , private helper: JwtHelperService) { }
ngOnInit() {
this.checkClaims();
}
checkClaims() {
// Do some logic
}
}
但是我无法对Checkclaim或ngoninit函数进行单元测试
我在beforeEach方法中添加了以下代码
TestBed.configureTestingModule({
declarations: [AccessPermissionDirective, DirectiveTestComponent],
providers: [
{provide: ElementRef, useClass: MockElementRef},
{provide: AuthenticationService, useClass: MockAuthenticationService},
JwtHelperService,
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
}).compileComponents();
fixture = TestBed.createComponent(DirectiveTestComponent);
component = fixture.componentInstance;
这里是组件。有权访问所有@Input变量。但是,无法访问ngOnInit和checkClaims函数。
任何想法我如何获得访问权限并分别进行测试。
我什至尝试在没有运气的情况下使用fixture.debugElement.query(By.directive(AccessPermissionDirective));
来获得它。