簡體   English   中英

類型錯誤:無法讀取未定義的屬性“管道” - Angular 單元測試

[英]TypeError: Cannot read property 'pipe' of undefined - Angular Unit test

我正在嘗試編寫單元測試,但出現此錯誤。

TypeError: Cannot read property 'pipe' of undefined at Jasmine

const mockServiceObj = jasmine.createSpyObj < FormRegistrationService > (["registerForm", "pipe"]);
const getCurrentSystemPreference = jasmine.createSpyObj < CurrentSystemPreferencesService > ([
    "getCurrentSystemPreference",
    "pipe",
]);

function getServiceManagerLinks(): ServiceManagerSharedLinksComponent {
    return new ServiceManagerSharedLinksComponent(null, mockServiceObj, getCurrentSystemPreference);
}

在此處輸入圖片說明

在此處輸入圖片說明

可能是適合您情況的正確(或至少接近正確)的單元測試方法。

您擁有currentSystemPreferencesService const 並將其提供給providers 現在它可以在所有測試用例中訪問。 您還提供getPreferences字段作為可調用方法。 of(true)意味着您返回Observable ,這是實際代碼中pipe()所需的,因為您在調用getPreferences(SystemPreference.APITestValue)后立即調用.pipe(...) getPreferences(SystemPreference.APITestValue)

describe('ServiceManagerSharedLinksComponent ', () => {
  let component: ServiceManagerSharedLinksComponent ;
  let fixture: ComponentFixture<ServiceManagerSharedLinksComponent>;

  const currentSystemPreferencesService: any = {
    getPreferences: () => of(true),
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ServiceManagerSharedLinksComponent],
      providers: [
        {
          provide: FormRegistrationService,
          useValue: {}, // <-- You will need a different const for registerForm() method
        },
        {
          provide: CurrentSystemPreferencesService,
          useValue: currentSystemPreferencesService,
        },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(ServiceManagerSharedLinksComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('should set display owner info when showOwnerInfo is TRUE', () => {
    expect(component.isDisplayOwnerInfoSetOnSystemPreference).toEqual(true);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM