用管道测试角度分量

时间:2020-10-29 16:55:41

标签: angular components pipe

我尝试测试一个组件,但是模板中包含了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停止它,否则它将永远不会停止... 当我取下管道时,我的测试会自动停止。 我试图对其进行嘲笑,但它也不起作用。

有人有主意吗?

谢谢

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

感谢您的回答。

当我在declarations数组中移动管道时,我遇到了同样的问题,测试永远不会停止。

我的应用程序中有很多组件,不能为每个测试模拟管道。

必须有某种方法来集成或导入自定义管道...:/