Ume*_*mer 6 unit-testing karma-jasmine angular
我正在为我的组件编写单元测试,但在创建组件实例时遇到问题并显示以下错误,
TypeError: Cannot read property 'patientId' of null
Run Code Online (Sandbox Code Playgroud)
我试过嘲笑所有的提供者,包括路由器和活动路由器我的 component.ts 是
export class PatientInsurancePlanSearchComponent implements OnInit {
private patientId: number = -1;
public selectedOrganizationInsurance: OrganizationInsurance;
public organizationInsurancePlans$: Observable<OrganizationInsurancePlan[]>;
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
private biilingHttpService: BillingHttpService
) {
this.selectedOrganizationInsurance = new OrganizationInsurance();
}
ngOnInit() {
this.patientId = history.state.patientId as number;
this.selectedOrganizationInsurance = history.state.selectedOrganizationInsurance as OrganizationInsurance;
this.organizationInsurancePlans$ = this.biilingHttpService.getOrganizationInsurancePlans(this.selectedOrganizationInsurance.id);
}
Run Code Online (Sandbox Code Playgroud)
规格
class FakeInsurancePlanSearchComponent {
@Input() public organizationInsurancePlans: OrganizationInsurancePlan[] = [];
@Input() public selectedOrganizationInsurance: OrganizationInsurance;
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ PatientInsurancePlanSearchComponent
, FakeInsurancePlanSearchComponent ],
imports: [
StoreModule.forRoot({}),
HttpClientModule,
RouterTestingModule,
],
providers: [
Store,
{
provide: ActivatedRoute, useValue: {
state: of({ selectedorganizationInsurancePlan: 'AETNA'})
}
},
BillingHttpService
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PatientInsurancePlanSearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
请指导我我缺少什么..
小智 7
如果您想将 PatientId 添加到浏览器会话历史堆栈中,您可以简单地使用:
history.pushState(state, title, url);
Run Code Online (Sandbox Code Playgroud)
在你的情况下,它应该是这样的:
describe(MyComponent.name, () => {
...
beforeEach(() => {
window.history.pushState({ patientId: 'somevalue'}, '', '');
...
})
it('...', () => {
})
}
Run Code Online (Sandbox Code Playgroud)
简单回答:
您可以在以下位置提供状态:
provideMockStore({ initialState: your_state })
Run Code Online (Sandbox Code Playgroud)
或者
mockStore.setState(your_state );
Run Code Online (Sandbox Code Playgroud)
但如果您有一家复杂的商店,我建议您执行以下操作:
MockStoreState。
type RecursivePartial<T> = {
[P in keyof T]?:
T[P] extends (infer U)[] ? RecursivePartial<U>[] :
T[P] extends object ? RecursivePartial<T[P]> :
T[P];
};
export class MockStoreState {
private store_a: RecursivePartial<Store_A>;
private store_b: RecursivePartial<Store_b>;
build(): any {
const defaultStore_a = {
...
};
const defaultStore_b = {
...
};
return {
store_a: { ...defaultStore_a , ...this.store_a},
store_b: { ...defaultStore_b , ...this.store_b },
};
}
setStore_a(value: RecursivePartial<Store_A>): Store_A_State {
this.store_a= value;
return this;
}
setStore_b(value: RecursivePartial<DatasourceState>): Store_B_State {
this.store_b= value;
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
describe(MyComponent.name, () => {
...
let mockStore: MockStore<any>;
beforeEach(() => {
...
mockStore = TestBed.get(Store);
})
it('...', () => {
const state = new MockStoreState().setStore_a({...})
.build();
mockStore.setState(state);
// HERE you have set the data in your store.
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3336 次 |
| 最近记录: |