【发布时间】:2021-01-22 15:31:03
【问题描述】:
我正在尝试编写单元测试,但出现此错误。
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);
}
【问题讨论】:
-
有点不寻常的单元测试(至少对我来说)。但是,您只是在模拟方法,但什么也不返回,因此
undefined。您应该只使用createSpy('spyName', 'getCurrentSystemPreference').and.returnValue(of({}));返回值 -
@GytisTG 感谢您的评论,是的,这很不寻常。我刚刚在上面发布了一个屏幕截图,我收到了错误,我该如何解决?再次感谢您的帮助,Gytis。
-
我提供了一个工作最小的例子。您可能需要更改/更新应用程序中的一些内容,因为并非所有代码部分都是可见的。但主要思想应该保持不变。
标签: angular unit-testing angular-unit-test