@input 속성을 가진 각도 구성 요소가 있으며 ngOnInit
. 일반적으로 @input을 단위 테스트 할 때 나는 그것을 그대로 제공 component.inputproperty=value
하지만이 경우에는 ngOnInit
. 이 입력 값을 .spec.ts
파일에 어떻게 제공합니까? 내가 생각할 수있는 유일한 옵션은 테스트 호스트 구성 요소를 만드는 것입니다.하지만 더 쉬운 방법이 있다면이 경로를 따르고 싶지 않습니다.
테스트 호스트 구성 요소를 수행하는 것은이를 수행하는 방법이지만 너무 많은 작업이 될 수 있음을 이해합니다.
ngOnInit
구성 요소의는 처음에 호출되는 fixture.detectChanges()
후 TestBed.createComponent(...)
.
따라서에 채워져 있는지 확인 ngOnInit
하려면 첫 번째 전에 설정하십시오 fixture.detectChanges()
.
예:
fixture = TestBed.createComponent(BannerComponent);
component = fixture.componentInstance;
component.inputproperty = value; // set the value here
fixture.detectChanges(); // first fixture.detectChanges call after createComponent will call ngOnInit
나는 모든이의 가정 beforeEach
및 당신이 다른 값을 원한다면 inputproperty
, 당신은 창조적 얻을 수 있어야 describe
s와 beforeEach
.
예를 들면 :
describe('BannerComponent', () => {
let component: BannerComponent;
let fixture: ComponentFixture<BannerComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({declarations: [BannerComponent]}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BannerComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeDefined();
});
describe('inputproperty is blahBlah', () => {
beforeEach(() => {
component.inputproperty = 'blahBlah';
fixture.detectChanges();
});
it('should do xyz if inputProperty is blahBlah', () => {
// test when inputproperty is blahBlah
});
});
describe('inputproperty is abc', () => {
beforeEach(() => {
component.inputproperty = 'abc';
fixture.detectChanges();
});
it('should do xyz if inputProperty is abc', () => {
// test when inputproperty is abc
});
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다