JHipster Karma测试:错误:没有JhiParseLinks的提供者!在spec / entry.ts中

时间:2018-01-25 16:06:45

标签: javascript angular unit-testing karma-jasmine jhipster

我是初学者,我正在使用Angular 4.3.2开展项目,该架构是使用JHipster构建的。 我找到了带有样品测试的测试文件夹。它们在我的代码之前成功运行。我试图为我创建的组件编写单元测试,但是当我执行测试时,我发现了这个错误:

Error: No provider for JhiParseLinks! in spec/entry.ts (line 2923)
    _throwOrNull@webpack:///node_modules/@angular/core/@angular/core.es5.js:2649:0 <- spec/entry.ts:2923:45
    _getByKeyDefault@webpack:///node_modules/@angular/core/@angular/core.es5.js:2688:0 <- spec/entry.ts:2962:37
    _getByKey@webpack:///node_modules/@angular/core/@angular/core.es5.js:2620:0 <- spec/entry.ts:2894:41
    get@webpack:///node_modules/@angular/core/@angular/core.es5.js:2489:0 <- spec/entry.ts:2763:30
    resolveNgModuleDep@webpack:///node_modules/@angular/core/@angular/core.es5.js:9475:0 <- spec/entry.ts:9749:28
    get@webpack:///node_modules/@angular/core/@angular/core.es5.js:10557:0 <- spec/entry.ts:10831:34
    resolveDep@webpack:///node_modules/@angular/core/@angular/core.es5.js:11060:0 <- spec/entry.ts:11334:48
    createClass@webpack:///node_modules/@angular/core/@angular/core.es5.js:10924:0 <- spec/entry.ts:11198:42
    createDirectiveInstance@webpack:///node_modules/@angular/core/@angular/core.es5.js:10744:21 <- spec/entry.ts:110

这是我的测试类,它建立在与Jhipster生成的其他测试类相同的模型上:

import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {UserMgmtComponent} from '../../../../../../main/webapp/app/admin';
import {UserService} from '../../../../../../main/webapp/app/shared';
import {Observable} from 'rxjs/Rx';
import {User} from '../../../../../../main/webapp/app/shared';
import {ActivatedRoute} from "@angular/router";
import {MockActivatedRoute} from "../../../helpers/mock-route.service";
import {DatePipe} from "@angular/common";
import {JhiDataUtils, JhiEventManager} from "ng-jhipster";

describe('Component Tests', () => {
        describe('UserManagementComponent', () => {
            let fixture: ComponentFixture<UserMgmtComponent>;
            let comp: UserMgmtComponent;
            let service: UserService;

            beforeEach(async(() => {
                TestBed.configureTestingModule({
                    declarations: [UserMgmtComponent],
                    providers: [
                        UserService,
                        JhiDataUtils,
                        DatePipe,
                        {
                            provide: ActivatedRoute,
                            useValue: new MockActivatedRoute({id: 123})
                        },
                        JhiEventManager
                    ]
                }).overrideTemplate(UserMgmtComponent, '')
                    .compileComponents();
            }));

            beforeEach(() => {
                fixture = TestBed.createComponent(UserMgmtComponent);
                comp = fixture.componentInstance;
                service = fixture.debugElement.injector.get(UserService);
            });

            describe('OnInit part', () => {
                it('Should call load all on init', () => {
                    spyOn(service, 'find').and.returnValue(Observable.of(new User(10)));
                    comp.ngOnInit();
                    expect(service.find).toHaveBeenCalledWith(123);
                });
            });
        });
    });

这是entry.ts的代码:

    import 'core-js';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/proxy';
import 'zone.js/dist/jasmine-patch';
import 'rxjs';
import 'intl/locale-data/jsonp/en-US.js';
import { TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

declare let require: any;
const testsContext: any = require.context('./', true, /\.spec/);
testsContext.keys().forEach(testsContext);

有没有人遇到过这个错误?

1 个答案:

答案 0 :(得分:0)

您的TestBed.configureTestingModule()调用在imports中定义的导入模块的arg对象中缺少test.module.ts属性,并且声明了许多服务的提供程序,包括JhiParseLinks

如果您的应用被命名为“MyApp”,则应添加以下行:

import { MyAppTestModule } from '../../../test.module';

        TestBed.configureTestingModule({
            imports: [MyAppTestModule],