我正在为一个有角度的组件编写测试用例,该组件对名为UserRecordFormService
的服务有很大的依赖性,该服务带有一个方法列表,对于提出http
请求的那些我已经创建了一个模拟类(以覆盖方法),并希望TestBed
扩展UserRecordFormService
本身的所有其他功能。
我从少数来源获得了参考,并且可以通过组件创建,但是它会引发所有其他测试规范的错误,并且还会使其他组件的测试规范失败。我在这里错过了什么吗,请帮助我确定我的错误。
describe('UserRecordFormComponent', () => {
let component: UserRecordFormComponent;
let fixture: ComponentFixture<UserRecordFormComponent>;
class UserRecordFormServiceStub {
//some methods returning data;
}
class AppLookupServiceStub {
//some methods returning data;
}
class AppRouterStub {
navigate() {}
}
const tb_base = {
imports: [
RouterTestingModule,
BrowserAnimationsModule,
RouterModule,
BrowserDynamicTestingModule
],
providers: [
UserRecordFormService,
AppHttpService,
HttpClient,
AppCookieService,
CookieService,
HttpHandler,
{ provide: AppLookupService, useClass: AppLookupServiceStub },
{ provide: ActivatedRoute, useValue: {
outlet: "primary",
data: {
actionId: "E",
buttonDesc: "Update"
},
params: of({
id: "0009"
}),
queryParams: of({
objectKey: "36353639373739393535353E352525252525253E3E3E3E363738363735363D3637363B353535"
}),
snapshot: {
data: {
actionId: "E",
buttonDesc: "Update"
},
queryParams: {
objectKey: "36353639373739393535353E352525252525253E3E3E3E363738363735363D3637363B353535"
}
}
}},
{ provide: Router, useClass: AppRouterStub }
],
declarations: [UserRecordFormComponent],
schemas: [NO_ERRORS_SCHEMA]
};
let UserRecordFormServiceStub = UserRecordFormServiceStub;
beforeEach(async(() => {
TestBed.configureTestingModule(tb_base)
.compileComponents();
}))
beforeEach(() => {
fixture = TestBed.createComponent(UserRecordFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
it('should create component', inject([UserRecordFormService], (loader: UserRecordFormServiceStub) => {
expect(component).toBeTruthy();
}));
it('should show expected output for ',inject([UserRecordFormService], (loader: UserRecordFormServiceStub) => {
expect(component.actionButtonDesc).toEqual("Update");
expect(component.subtypeName).toEqual("0 - Main bank");
}));
})
我从这里引用了以下内容:https://github.com/angular/quickstart/issues/320 https://github.com/angular/angular/issues/10943
错误:“未捕获的TypeError:无法读取未定义抛出的属性'消息'”,是它从其他组件测试规范即UserListComponent
抛出的错误。注入是否会对其他组件造成任何麻烦。
请让我知道我是否在这里丢失了一些东西。