我无法从 Angular - Karma/Jasmine 单元测试中的规范文件访问组件
I can't reach the component from the spec file in Angular - Karma/Jasmine unit tests
我正尝试 运行 在 Angular 中进行简单的单元测试,但它失败了,我无法从 spec 文件访问该组件。它说找不到未定义的 属性,我不知道 mistake.this 是我要测试的组件中的函数:
factorOrQuickpay(): void {
if (this.load.factor) {
this.factor = true;
this.resourceType = TableResources.Factor;
} else if (!this.load.factor) {
this.factor = false;
this.resourceType = TableResources.Quickpay;
} else if(!this.load.factor && !this.load.quickpay) {
this.none = true;
}
}
这是 .spec 文件:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LoadResourcesComponent } from './load-resources.component';
describe('LoadResourcesComponent', () => {
let component: LoadResourcesComponent;
let fixture: ComponentFixture<LoadResourcesComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [LoadResourcesComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(LoadResourcesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fdescribe('factorOrQuickpay', () => {
beforeEach(() => {
spyOn(component, 'factorOrQuickpay');
});
it('shows Factor Load scenario', () => {
component.factorOrQuickpay()
expect(component.factor).toBe(true);
});
});
});
你能帮我找出错误在哪里吗?
错误信息是“TypeError: Cannot read 属性 'factor' of undefined.”
谢谢。
我认为您的节点版本是 15.0。将您的节点版本降级到节点 14.0 即可。
我正尝试 运行 在 Angular 中进行简单的单元测试,但它失败了,我无法从 spec 文件访问该组件。它说找不到未定义的 属性,我不知道 mistake.this 是我要测试的组件中的函数:
factorOrQuickpay(): void {
if (this.load.factor) {
this.factor = true;
this.resourceType = TableResources.Factor;
} else if (!this.load.factor) {
this.factor = false;
this.resourceType = TableResources.Quickpay;
} else if(!this.load.factor && !this.load.quickpay) {
this.none = true;
}
}
这是 .spec 文件:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LoadResourcesComponent } from './load-resources.component';
describe('LoadResourcesComponent', () => {
let component: LoadResourcesComponent;
let fixture: ComponentFixture<LoadResourcesComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [LoadResourcesComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(LoadResourcesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fdescribe('factorOrQuickpay', () => {
beforeEach(() => {
spyOn(component, 'factorOrQuickpay');
});
it('shows Factor Load scenario', () => {
component.factorOrQuickpay()
expect(component.factor).toBe(true);
});
});
});
你能帮我找出错误在哪里吗?
错误信息是“TypeError: Cannot read 属性 'factor' of undefined.”
谢谢。
我认为您的节点版本是 15.0。将您的节点版本降级到节点 14.0 即可。