【发布时间】:2020-06-26 13:30:15
【问题描述】:
我想在Angular/Jasmine中介绍RXJS的管道功能。
我收到此错误:
TypeError: this.element.children.pipe is not a function
代码:
ngOnInit() {
this.element.children.pipe(
mapObservableArray((children: ObservableTree<ItemModel>) => children.value.properties))
.subscribe((props: Properties[]) => {
// do something with the props or children
this.childrenElements = props as any;
});
}
这确保我可以检索孩子的值并将其保存在 this.childrenElements 上。
.spec 文件:
import { CommonModule } from '@angular/common';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { myComponent } from './my.component';
describe('myComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
declarations: [myComponent],
providers: [],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
});
it('should create the component', async () => {
const fixture = TestBed.createComponent(myComponent);
const componentInst = fixture.componentInstance;
componentInst.childrenElements = [
{ name: 'name1' },
{ name: 'name2' }
];
// when
componentInst.ngOnInit();
// then
expect(componentInst).toBeDefined();
});
});
尝试存根:
spyOn(componentInst.element.children, 'pipe').and.stub();
但这没有帮助。
如何覆盖我的这部分代码?
【问题讨论】:
标签: angular unit-testing rxjs jasmine