Jasmine 单元测试在所有测试 运行 时失败,在隔离 运行 时成功
Jasmine Unit Test failing when ran with all tests, succeeds when ran in isolation
我有一个 angular 8 项目并且有一个开箱即用的基本 "should create" 单元测试失败并出现错误 "Uncaught TypeError: Cannot read property 'forEach' of null thrown"。问题是,如果 运行 孤立地进行,此测试会成功,但当 运行 与项目的所有其他单元测试一起时,此测试会失败。此外,在单元测试中创建的组件不包含任何 forEach 函数。
spec.ts 有问题
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SubheaderComponent } from './subheader.component';
import { requiredTestModules } from '../../testing/import.helpers';
import { ApplicationInsightsService } from '../../services/application-insight/app-insight.service';
describe('SubheaderComponent', () => {
let component: SubheaderComponent;
let fixture: ComponentFixture<SubheaderComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SubheaderComponent ],
imports: [
requiredTestModules
],
providers: [ApplicationInsightsService]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SubheaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
使用 forEach 代码未实现其他组件:
component.ts
console.log(selectedInvoices);
this.selectedInvoices = selectedInvoices;
let subtotal = 0;
this.selectedInvoices.forEach((selectedInvoice) => {
subtotal = +subtotal + +selectedInvoice.paymentAmt;
});
this.Subtotal = subtotal;
this.fee = this.feePercent / 100 * this.Subtotal;
this.totalPayment = this.Subtotal + this.fee;
console.log(this.selectedInvoices);
我确实有其他组件使用 forEach,但命中那些 forEach() 的单元测试在隔离和 运行 全部成功。
任何 help/suggestions 将不胜感激。
看起来导致错误的 forEach 在失败的组件测试之前被调用。添加空检查使我的测试成功。
更新了 component.ts 代码:
if (this.selectedInvoices) {
this.selectedInvoices = selectedInvoices;
let subtotal = 0;
this.selectedInvoices.forEach((selectedInvoice) => {
subtotal = +subtotal + +selectedInvoice.paymentAmt;
});
this.Subtotal = subtotal;
this.convenienceFee = this.convenienceFeePercent / 100 * this.Subtotal;
this.totalPayment = this.Subtotal + this.convenienceFee;
console.log(this.selectedInvoices);
}
如果错误是针对与错误存在的地方不同的组件抛出的,似乎很难调试,但这听起来像是一个更大的业力问题。感谢@Archit Garg 的帮助。
我有一个 angular 8 项目并且有一个开箱即用的基本 "should create" 单元测试失败并出现错误 "Uncaught TypeError: Cannot read property 'forEach' of null thrown"。问题是,如果 运行 孤立地进行,此测试会成功,但当 运行 与项目的所有其他单元测试一起时,此测试会失败。此外,在单元测试中创建的组件不包含任何 forEach 函数。
spec.ts 有问题
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SubheaderComponent } from './subheader.component';
import { requiredTestModules } from '../../testing/import.helpers';
import { ApplicationInsightsService } from '../../services/application-insight/app-insight.service';
describe('SubheaderComponent', () => {
let component: SubheaderComponent;
let fixture: ComponentFixture<SubheaderComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SubheaderComponent ],
imports: [
requiredTestModules
],
providers: [ApplicationInsightsService]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SubheaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
使用 forEach 代码未实现其他组件:
component.ts
console.log(selectedInvoices);
this.selectedInvoices = selectedInvoices;
let subtotal = 0;
this.selectedInvoices.forEach((selectedInvoice) => {
subtotal = +subtotal + +selectedInvoice.paymentAmt;
});
this.Subtotal = subtotal;
this.fee = this.feePercent / 100 * this.Subtotal;
this.totalPayment = this.Subtotal + this.fee;
console.log(this.selectedInvoices);
我确实有其他组件使用 forEach,但命中那些 forEach() 的单元测试在隔离和 运行 全部成功。
任何 help/suggestions 将不胜感激。
看起来导致错误的 forEach 在失败的组件测试之前被调用。添加空检查使我的测试成功。
更新了 component.ts 代码:
if (this.selectedInvoices) {
this.selectedInvoices = selectedInvoices;
let subtotal = 0;
this.selectedInvoices.forEach((selectedInvoice) => {
subtotal = +subtotal + +selectedInvoice.paymentAmt;
});
this.Subtotal = subtotal;
this.convenienceFee = this.convenienceFeePercent / 100 * this.Subtotal;
this.totalPayment = this.Subtotal + this.convenienceFee;
console.log(this.selectedInvoices);
}
如果错误是针对与错误存在的地方不同的组件抛出的,似乎很难调试,但这听起来像是一个更大的业力问题。感谢@Archit Garg 的帮助。