首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对模拟子组件的角分量测试“无法绑定到输入”警告

对模拟子组件的角分量测试“无法绑定到输入”警告
EN

Stack Overflow用户
提问于 2020-09-16 18:32:12
回答 1查看 593关注 0票数 1

我面临一个问题,就是使用@ngneat/spectator在角9组件测试中模拟子组件。模拟和传递模拟很好,但是它在输入的日志中抛出警告(即使它们是有效的)。

简化的组件如下:

代码语言:javascript
复制
@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';
}

现在是观众测试

代码语言:javascript
复制
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');
    });
  });

});

测试运行良好,并通过了。然而,日志打印警告:

代码语言:javascript
复制
console.warn
    Can't bind to 'someInput' since it isn't a known property of 'child'.

知道它为什么要记录警告吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-29 16:39:14

发现了我自己的问题--原来createComponentFactory()必须在beforeEach()之外被调用。

一旦我把它移出beforeEach块,模拟就开始像预期的那样工作了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63926015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档