我面临一个问题,就是使用@ngneat/spectator在角9组件测试中模拟子组件。模拟和传递模拟很好,但是它在输入的日志中抛出警告(即使它们是有效的)。
简化的组件如下:
@Component({
selector: 'child',
template: '<h2>{{someInput}}</h2>'
})
export class ChildComponent {
@Input() someInput: string;
}
@Component({
selector: 'parent',
template: '<child [someInput]="inputVal"></child>'
})
export class ParentComponent {
public inputVal = 'hello';
}现在是观众测试
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { MockComponent } from 'ng-mocks';
...
describe('ParentComponent', () => {
let spectator: Spectator<ParentComponent>;
let createComponent: SpectatorFactory<ParentComponent>;
beforeEach(() => {
createComponent = createComponentFactory({
component: ParentComponent,
declarations: [MockComponent(ChildComponent)]
});
spectator = createComponent();
});
describe('example', () => {
it('should set the input', () => {
expect(spectator.query(ChildComponent).someInput).toEqual('hello');
});
});
});测试运行良好,并通过了。然而,日志打印警告:
console.warn
Can't bind to 'someInput' since it isn't a known property of 'child'.知道它为什么要记录警告吗?
发布于 2020-09-29 16:39:14
发现了我自己的问题--原来createComponentFactory()必须在beforeEach()之外被调用。
一旦我把它移出beforeEach块,模拟就开始像预期的那样工作了。
https://stackoverflow.com/questions/63926015
复制相似问题