Angular中针对条件数据的jasmine测试用例的问题



我正在学习用jasmine为angular应用程序编写测试用例,我的html中有一个组件,如下

<app-image [imgData]="pageData['configData']['image']"></app-image>

我的测试用例如下

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PlainTextPipe } from '@pipes/plain-text.pipe';
import { DataService } from '@services/data.service';
import { BasicComponent } from './basic.component';
fdescribe('BasicComponent', () => {
let component: BasicComponent;
let fixture: ComponentFixture<BasicComponent>;
let pageData;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [BasicComponent],
providers: [PlainTextPipe]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BasicComponent);
component = fixture.componentInstance;
spyOn(component, 'renderPage');
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});


it('should initialize', () => {
component.ngOnInit();
fixture.detectChanges();
});
});

当我运行这个测试用例时,我得到了无法读取未定义的属性"image",因为在某些场景中我没有图像,我如何在测试用例中处理它,因为在特定场景中我会有图像,而在其他情况下我不会,因为这是通用组件

问题是pageData.configData未定义。

尝试:

it('should create', () => {
component.pageData.configData.image = 'hello'; // not sure if it's a string but you know how to mock it
expect(component).toBeTruthy();
});

最新更新