Angular 10单元测试-组件实例返回未定义



Am在组件实例上获得未定义的值。在我从角度8升级到角度10之前,它一直在工作。我在测试中唯一更改的是,async((改为waitForAsync((,因为async已被弃用。

describe('ProtocolInfoComponent', () => {
let component: ProtocolInfoComponent;
let fixture: ComponentFixture<ProtocolInfoComponent>;

const mockActivatedRoute = new MockActivatedRoute({
parent: new MockActivatedRoute({
params: of({ studyId: 1001 })
})
});

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [FormsModule,
AppConfigModule,
SharedModule,
HttpClientTestingModule
],
declarations: [ ProtocolInfoComponent ],
providers: [
{ provide: UserService, useClass: UserServiceStub },
{ provide: APP_CONFIG_STUB, useClass: APP_DI_CONFIG_STUB },
{ provide: StudySetupService, useClass: StudySetupServiceStub },
{ provide: StudyCreateService, useClass: StudyCreateServiceStub },
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
{ provide: Router, useClass: RouterStub }]
})
.compileComponents().then(() => {
AppInjector.setInjector(TestBed.inject(Injector));
});
}));

beforeEach(() => {
console.log('TestBed', TestBed.createComponent(ProtocolInfoComponent));
fixture = TestBed.createComponent(ProtocolInfoComponent);
console.log('Fixture',fixture);
component = fixture.componentInstance;
console.log('Component',component);
fixture.detectChanges();
});

it('should create', () => {
console.log('=====component=====',component)
expect(component).toBeTruthy();
});

it('should have the study', () => {
expect(component.study).not.toBe(null);
});

it('should have the responsibiltiies', () => {
expect(component.responsibilities).not.toBe(null);
expect(component.orgResponsibilities).not.toBe(null);
});
});

我在测试中使用了控制台语句来查看fixture和component的值。两者都未定义。它适用于其他测试,我也有同样的测试关于如何解决这个问题的任何想法。

终于能够弄清楚了。在providers数组中,其中一个provider是useValue,而不是useClass;

{ provide: APP_CONFIG_STUB, useValue: APP_DI_CONFIG_STUB }

相关内容

  • 没有找到相关文章

最新更新