【问题标题】:Test angular component with pipe用管子测试角分量
【发布时间】:2021-02-12 04:04:42
【问题描述】:

我尝试测试一个组件,但在模板中使用 cutom 管道

component.html:

<ul class="test">
    <li class="test1">{{'LABEL_TEST_PIPE' | translateLabels }}</li>
</ul>

component.spec.ts

describe('Component', () => {
  let fixture: ComponentFixture<Component>;

  beforeEach(async () => {
    TestBed.configureTestingModule({
      declarations: [ Component ],
      imports: [ TranslateLabelsPipe ]
    });

  });

  test('should be translate', () => {
    fixture = TestBed.createComponent(Component);
    const component = fixture.componentInstance;
    fixture.detectChanges();
    const element = fixture.debugElement;
    expect(element.nativeElement.querySelector('li').textContent).toContain('test pipe translate');
  });
});

我的终端里有这个:

Test Suites: 0 failed, 0 of 1 total
Tests:       0 total
Snapshots:   0 total
Time:        284 s

我必须用 Ctrl+C 停止它,否则它永远不会停止... 当我移除管道时,我的测试工作并自行停止。 我试图模拟它,但它也不起作用。

有人知道吗?

谢谢

【问题讨论】:

    标签: angular components pipe


    【解决方案1】:

    您可以使用以下方法模拟管道:https://stackoverflow.com/a/41826482/7365461,但您的主要问题是您将管道放入 imports 数组而不是 declarations。

    尝试将管道移动到declarations 数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2020-04-21
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多