我想要进行单元测试并获得所有代码的覆盖率,但我无法获得订阅内存在的代码的覆盖率我能够监视服务和功能,但在订阅内我无法进行单元测试并获得代码覆盖率。以下是 Angular 7 代码。
LoadListData(type) {
this.itemListEnvName = [];
if (type === 'EnvirnmentList') {
this.environmentBookingService.getBookedEnv()
.subscribe(
(environmentBookingsData: EbEnvironmentBooking[]) => {
if (environmentBookingsData.length > 0) {
this.itemListEnvNameList = environmentBookingsData;
this.itemListEnvName = [];
this.itemListEnvNameList.forEach(element => {
const obj = {};
obj['id'] = element['environmentId'];
obj['itemName'] = element['environmentName'];
this.itemListEnvName.push(obj);
this.generateCheckDisable = false;
});
} else {
this.generateCheckDisable = true;
}
},
(error) => {
this.showMessage('No Response From Delivery DB API');
}
);
} else {
this.showMessage('No Response From Delivery DB API');
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试用例内的代码就像 …
我收到错误,因为在单元测试时无法读取 angular 7 中激活路由的未定义属性“订阅”。我已经尝试了多种方式,请帮忙。
Observer.of 对我不起作用,因为我有 rx.js 版本 6.3.3
这是我的组件文件 ChildComponent.ts
import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-child',
template: `
<h1>Enter your name</h1>
<form
[formGroup]="nameForm"
(ngSubmit)="submitForm(nameForm)"
>
<input
type="text"
formControlName="firstName"
placeholder="first"
>
<input
type="text"
formControlName="lastName"
placeholder="last"
>
<button type="submit">launch to space</button>
</form>
`,
styleUrls: ['./child.component.css']
})
export class ChildComponent {
@Input() nameForm: FormGroup;
private abc: number;
constructor(
public route: ActivatedRoute,
) {
this.route.queryParams.subscribe(params => …
Run Code Online (Sandbox Code Playgroud)