代码之家  ›  专栏  ›  技术社区  ›  Codehan25

TypeError:无法读取未定义的属性“subscribe”(角度单元测试)

  •  0
  • Codehan25  · 技术社区  · 5 年前

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    import { BreadcrumbsComponent } from './breadcrumbs.component';
    import { RouterTestingModule } from '@angular/router/testing';
    import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
    import { HttpClient } from '@angular/common/http';
    import { HttpClientTestingModule } from '@angular/common/http/testing';
    import { BreadcrumbsService } from './breadcrumbs.service';
    import { multiTranslateHttpLoaderFactory } from 'app/app.component';
    
    fdescribe('BreadcrumbsComponent', () => {
      let component: BreadcrumbsComponent;
      let translateService: TranslateService;
      let fixture: ComponentFixture<BreadcrumbsComponent>;
      let translateServiceStub: Partial<TranslateService>;
    
    
    let translateServiceStub: Partial<TranslateService>;
    
    translateServiceStub = {
      getBrowserLang: () => {
        return 'de';
      },
      onLangChange: new EventEmitter(),
    };
    
    breadcrumbsServiceStub = {
      onUpdateRequest: new Subject(),
      updateBreadcrumbs: () => {
      }
    };
    
      beforeEach((() => {
        TestBed.configureTestingModule({
          declarations: [BreadcrumbsComponent],
          imports: [
            RouterTestingModule,
            HttpClientTestingModule,
            TranslateModule.forRoot({
              loader: {
                provide: TranslateLoader,
                useFactory: multiTranslateHttpLoaderFactory,
                deps: [HttpClient]
              }
            }),
          ],
          providers: [
            {provide: TranslateService, useValue: translateServiceStub},
            {provide: BreadcrumbsService, useValue: breadcrumbsServiceStub},
          ]
        });
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(BreadcrumbsComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    TypeError:无法读取未定义的属性“subscribe”

    export class BreadcrumbsComponent implements OnInit {
    
      breadcrumbs: Breadcrumb[] = [];
    
      constructor(private router: Router,
                  private activeRoute: ActivatedRoute,
                  private translate: TranslateService,
                  private breadcrumbsService: BreadcrumbsService) {
      }
    
      ngOnInit() {
        this.translate.onLangChange.subscribe(() => this.identifyRoute());
      }
    

    ...

    我做错什么了?或有没有一种方法可以绕过这个错误或者模拟这条线以便测试通过?我真的不知道如何在考试中模仿这句话。

    0 回复  |  直到 5 年前
        1
  •  -1
  •   Shashank Vivek    5 年前

    import { of } from 'rxjs';
    
    translateServiceStub = {
        getBrowserLang: () => {
           return 'de';
        },
        onLangChange: of({
           // whatever object is expected
         }),
     };