angular - TypeError:无法读取未定义的属性“管道”-Angular 单元测试
问题描述
我正在尝试编写单元测试,但出现此错误。
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()
.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);
});
});
推荐阅读
- css - 折叠时如何防止引导导航栏分成几行
- javascript - React jsx三元运算符多个值
- mysql - django模型中可以有多少个字段?
- oop - (Dart)私有和非私有类构造函数中私有和非私有初始化器的作用?
- c# - 无论方向如何,如何逐渐向对象添加更多力以提高其速度?
- scala - 在 Flink 中处理的数据不保存在 Cassandra 数据库中
- r - 如何在 R Shiny 中准备分段仪表板?
- documentation - 在新项目中理解业务逻辑的最快方法
- java - 如何构建可执行的 JavaFx Jar 文件
- python - 如何将压缩文件夹中的 CSV 文件从 URL 加载到 Pandas DataFrame