【发布时间】:2021-01-28 09:27:45
【问题描述】:
失败:无法读取 null 的属性“queryParams” 在
我假设这是因为我在 ngOnInit() 中有以下内容:
ngOnInit() {
this.route.queryParams.subscribe(async params => {
this.userInfo = await JSON.parse(params['user_info']);
});
到目前为止,我已经尝试使用以下内容构建我的单元测试:
describe('AddItineraryPage', () => {
let component: AddItineraryPage;
let fixture: ComponentFixture<AddItineraryPage>;
let routeStub;
beforeEach(async(() => {
routeStub = null;
TestBed.configureTestingModule({
declarations: [ AddItineraryPage ],
imports: [IonicModule.forRoot(), FormsModule, ReactiveFormsModule, RouterTestingModule],
providers: [
{provide: ActivatedRoute, useValue: routeStub}
]
}).compileComponents();
fixture = TestBed.createComponent(AddItineraryPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
routeStub.queryParams = {
displayName: '',
dateOfBirth: '',
email: '',
photos: [],
location: '',
bio: '',
intDestination: [],
userId: ''};
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component).toBeTruthy();
});
});
});
【问题讨论】:
-
你为什么使用
JSON.parse?不是queryParams' 值应该是(简单)字符串? -
@AndreiGătej 我将一个对象作为 queryParams 传递,您必须使用 JSON.stringify 来传递一个对象。因此,一旦我进入页面,我就必须对其进行解析以使其再次成为对象。
标签: angular typescript angular-routing angular-router angular2-testing