错误:无法解析HttpRequest

时间:2019-01-30 13:16:34

标签: angular typescript jasmine karma-jasmine

发现了几个类似的错误,但我认为它们并不适用。他们谈论组件的顺序,我想我已经尝试了一切。

我正在为现有服务编写新的单元测试。这是服务构造函数:

@Injectable()
export class CreateSpAuthorizationUtility {
    constructor(
        private dialogService: DialogService,
        private router: Router,
        private store: Store<AppStore>,
        private noteService: NoteService,
        private stepperService: StepperService
    ) {
    }
}

这是我写的单元测试:

describe('Given a CreateSpAuthorizationUtility', () => {

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
            ],
            imports: [
                HttpClientTestingModule
            ],
            providers: [
                AppConfig,
                HttpRequest,
                CreateSpAuthorizationUtility,
                {provide: DialogService, useClass: MockDialogService},
                {provide: NoteService, useClass: MockNoteService},
                {provide: Router, useClass: MockRouter},
                {provide: StepperService, useClass: StepperServiceMock},
                {provide: Store, useClass: TestStore}
            ]
        });
    }));

    it('should build query params',
        inject([CreateSpAuthorizationUtility, HttpTestingController, AppConfig],
            fakeAsync( (service: CreateSpAuthorizationUtility,
                        mockBackend: HttpTestingController,
                        appConfig: AppConfig,
                        router: MockRouter,
                        store: TestStore<AppStore>,
                        dialogService: MockDialogService,
                        noteService: MockNoteService,
                        stepperService: StepperServiceMock)  => {

                const prescreen: SpPrescreenSelectedValues = SP_PRESCREEN_SELECTED_VALUES_MOCK;
                service.buildQueryParams(prescreen, false);
            })));
});

这是MockNoteService:

@Injectable()
export class MockNoteService extends NoteService {
    public static NON_EXISTING_ID = 'nonExistingId';

    public getNoteDefinition(noteDefinitionVisibleId: string): Observable<NoteDefinition> {
        if (noteDefinitionVisibleId === MockNoteService.NON_EXISTING_ID) {
            return observableThrowError('Test');
        }

        return of({
            id: '',
            version: 0,
            name: '',
            description: '',
            attributeDefs: [],
            visibleId: '',
            userCreatable: false,
            instanceCopyable: false,
            definitionEditable: false,
            isSystem: false,
            bodyRequired: true
        });
    }
}

哪些扩展了NoteService:

export class NoteService extends BaseService {
    constructor(private http: HttpRequest, private appConfig: AppConfig, private store: Store<AppStore>) {
        super();
    }
}

运行它时,出现以下错误:

Error: Can't resolve all parameters for HttpRequest: (?, ?, ?, ?).
    at syntaxError (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:1021:1)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:10922:1)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:10815:1)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getInjectableTypeMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:11037:1)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:11046:1)
    at http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:10984:1
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getProvidersMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:10944:1)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:10663:53)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:23876:1)

正如我所说,我已经尝试订购项目并与项目中的其他单元测试进行比较。我不确定下一步该怎么做。

1 个答案:

答案 0 :(得分:0)

因此,最后,我使MockNoteService不从NoteService扩展,因此我没有继承链,这似乎可以解决问题。

感谢Buczkowski,谢谢您的帮助。