Angular 中条件数据的 jasmine 测试用例问题
Issue with jasmine test cases in Angular for conditional data
我正在学习用 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' of undefined 因为在某些情况下我没有图像我如何在我的测试用例中处理它,因为在特定情况下我将拥有图像,而在其他情况下我不会,因为这是公共组件
问题是 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();
});
我正在学习用 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' of undefined 因为在某些情况下我没有图像我如何在我的测试用例中处理它,因为在特定情况下我将拥有图像,而在其他情况下我不会,因为这是公共组件
问题是 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();
});