![](/img/trans.png)
[英]Angular 8 - Unit Test Issue TypeError: Cannot read property 'name' of undefined
[英]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.