1

我已将 AngularJS 组件“contactBox”升级为“contactBoxDirective”并在 Angular 组件“personInfo”中使用。

但是,在使用 TestBed 运行测试用例时,我总是遇到错误

错误:[$injector:unpr] 未知提供者:contactBoxDirectiveProvider <-contactBoxDirective http://errors.angularjs.org/1.6.9/53) 在contactBoxDirective.UpgradeComponent (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188.browserify:116364:23) 在新的contactBoxDirective (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea33a7a188.browserify: :23) 在 createClass (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188.browserify:93137:20) 预计未定义是真实的。在用户上下文。(C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188.browserify:2400:27) 在 ZoneDelegate.invoke (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188.browserify:246922:26) atProxyZoneSpec .onInvoke (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188.browserify:246437:39) 在 ZoneDelegate.invoke (C:/Users/user/AppData/Local/Temp/3ac750590575e0af0efa9c7ea3a7a188)。

测试代码 -

describe('PersonModalContentComponent', () => {
  let $injector: angular.auto.IInjectorService;
  let component: PersonModalContentComponent;
  let fixture: ComponentFixture<PersonModalContentComponent>;


  beforeAll(() => {
    // First, initialize the Angular testing environment.
    getTestBed().resetTestEnvironment();
    getTestBed().initTestEnvironment(
        BrowserDynamicTestingModule,
        platformBrowserDynamicTesting()
    );
  });

  beforeEach(angular.mock.inject((_$injector_: ng.auto.IInjectorService) => {
    $injector = _$injector_;
  }));

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [ {
        provide: '$injector',
        useFactory: () => $injector,
        deps: []
      }, NgbModal, NgbActiveModal ],
      imports: [
        TestsModule,
        NgbModule.forRoot(),
        TranslateModule.forRoot(),
        PersonDetailsModule
      ]
    })
    .compileComponents();
  });


  beforeEach(() => {
    fixture = TestBed.createComponent(PersonModalContentComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });


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

});
4

0 回答 0