简体   繁体   English

用管子测试角部件

[英]Test angular component with pipe

I try to test a component but with cutom pipe in template我尝试测试一个组件,但在模板中使用 cutom 管道

component.html :组件.html

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

component.spec.ts组件.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');
  });
});

I have this in my terminal :我的终端中有这个:

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

I have to stop it with Ctrl+C otherwise it never stops... When I remove the pipe, my test works and stops itself.我必须用 Ctrl+C 停止它,否则它永远不会停止......当我移除管道时,我的测试工作并自行停止。 I tried to mock it but it doesn't work neither.我试图嘲笑它,但它也不起作用。

Someone has any idea ?有人有什么想法吗?

Thanks谢谢

You can mock the pipe using this methodology: https://stackoverflow.com/a/41826482/7365461 but your main issue is that you put the pipe in imports array instead of declarations .您可以使用这种方法来模拟管道: https : //stackoverflow.com/a/41826482/7365461但您的主要问题是您将管道放在imports数组而不是declarations

Try moving the pipe to declarations array.尝试将管道移动到declarations数组。

thanks for your answer.感谢您的回答。

When I move the pipe in declarations array, I have the same issue, the test never stops.当我在declarations数组中移动管道时,我遇到了同样的问题,测试永远不会停止。

I have a lot of components in my app, I can't mock pipe for every test.我的应用程序中有很多组件,我无法为每个测试模拟管道。

There must be some way to integrate or import a custom pipe... :/必须有某种方法来集成或导入自定义管道...:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM