Angular 条件数据中jasmine测试用例的问题
我正在学习用jasmine为angular应用程序编写测试用例,我的html中有一个组件,如下所示Angular 条件数据中jasmine测试用例的问题,angular,jasmine,tdd,karma-runner,Angular,Jasmine,Tdd,Karma Runner,我正在学习用jasmine为angular应用程序编写测试用例,我的html中有一个组件,如下所示 <app-image [imgData]="pageData['configData']['image']"></app-image> 我的测试用例如下所示 import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { PlainTextPipe
<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();
});
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从'@pipes/plain text.pipe'导入{PlainTextPipe};
从'@services/data.service'导入{DataService};
从“/basic.component”导入{BasicComponent};
fdescribe('BasicComponent',()=>{
let组分:基本组分;
let夹具:组件夹具;
让页面显示数据;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[基本组件],
提供者:[明文管道]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(基本组件);
组件=fixture.componentInstance;
spyOn(组件“渲染页面”);
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该初始化',()=>{
组件。ngOnInit();
fixture.detectChanges();
});
});
当我运行这个测试用例时,我得到无法读取未定义的属性“image”,因为在某些场景中,我没有图像,我如何在测试用例中处理它,因为在特定场景中,我会有图像,而在其他情况下,我不会,因为这是公共组件
问题在于页面数据。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();
});