1

我在使用 @ngneat/spectator 在 Angular 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'.

任何想法为什么它会记录警告?

4

1 回答 1

2

发现我自己的问题 - 结果createComponentFactory()必须在beforeEach().

一旦我将它移出beforeEach街区,模拟就会开始按预期工作。

于 2020-09-29T16:39:14.230 回答