提问人:Kashif999 提问时间:11/15/2023 更新时间:11/15/2023 访问量:19
无法覆盖 Angular 方法中的特定行
Unable to cover specific lines in Angular method
问:
我有这个类,我使用几个服务来获取数据,然后转换它并将其作为可观察的返回。无论我尝试什么,测试覆盖率都不仅仅涵盖其中的一个特定部分。尝试了各种不同的东西,但它没有覆盖。这是类:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import { AccountDetail } from '../../models/accounts';
import { environment } from '../../../../environments/environment';
import { AccountDescriptionHelper } from '../../utils/account-subaccount';
import { GroupService } from '../group-structure';
import { TreeNode } from '../../models/treeNode';
@Injectable({
providedIn: 'root'
})
export class AccountsChartService {
private readonly ENDPOINT = `${environment}/ac`;
constructor(
private http: HttpClient,
private groupService: GroupService,
private accountDescriptionHelper: AccountDescriptionHelper
) {}
loadAccountByChartService(bT: string, cT: string): Observable<Node[]> {
const accountSubaccountDescEP = `${this.ENDPOINT}/cT/${cT}/ad`;
let aCBS = new Subject<Node[]>();
let aSAD = new Subject<AccountDetail[]>();
this.http
.get<AccountDetail[]>(accountSubaccountDescEP)
.subscribe(response => {
this.accountDescriptionHelper.storeMapFromDescriptionList(response);
aSAD.next(response);
});
aSAD.subscribe(list => {
this.groupService.getGroupedAccounts(cT, bT, list).subscribe(response => {
aCBS.next(response);
});
});
return aCBS.asObservable();
}
}
以下是测试:
it('should be created', () => {
expect(service).toBeTruthy();
});
it('test', () => {
httpClient = createHttpClient();
service = new AccountsChartService(httpClient as HttpClient, groupService, asdHelper);
const a = spyOn(groupService, 'getGroupedAccounts')
const result = service.testMethod('1', 'Test');
expect(result).toBeDefined();
result.subscribe(() => {
expect(a).toHaveBeenCalled();
});
});
});
它说这个特定的部分被发现,但我不知道我如何断言和操纵 aCBS 和 aSAD。我尝试将它们作为类实例变量移动,但同样的事情。只是不知道该怎么做。感谢您的观看。希望得到解决。
它说这部分是未覆盖的,其他一切都显示为覆盖:
list => {
this.groupService.getGroupedAccounts(cT, bT, list).subscribe(response => {
aCBS.next(response);
});
}
答:
0赞
MoxxiManagarm
11/15/2023
#1
你那里有很多(潜在的)问题。首先,重写你的方法。
loadAccountByChartService(bT: string, cT: string): Observable<Node[]> {
const accountSubaccountDescEP = `${this.ENDPOINT}/cT/${cT}/ad`;
return this.http.get<AccountDetail[]>(accountSubaccountDescEP).pipe(
tap(response => this.accountDescriptionHelper.storeMapFromDescriptionList(response)),
switchMap(response => this.groupService.getGroupedAccounts(cT, bT, response)),
);
}
现在是你的测试用例。异步代码是异步代码,无论是在服务、组件还是单元测试中。在单元测试方法结束后调用 suscription 块中的 expect。有几种方法可以处理异步单元测试。在您的案例中,最简单的更改是简单地添加回调。done
it('test', done => {
httpClient = createHttpClient();
service = new AccountsChartService(httpClient as HttpClient, groupService, asdHelper);
const a = spyOn(groupService, 'getGroupedAccounts')
const result = service.testMethod('1', 'Test');
expect(result).toBeDefined();
result.subscribe(() => {
expect(a).toHaveBeenCalled();
done();
});
});
});
但是,我建议在测试异步代码时接触大理石测试。
评论