繁体   English   中英

用管子测试角部件

[英]Test angular component with pipe

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

组件.html

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

组件.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 停止它,否则它永远不会停止......当我移除管道时,我的测试工作并自行停止。 我试图嘲笑它,但它也不起作用。

有人有什么想法吗?

谢谢

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

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

感谢您的回答。

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

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

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

暂无
暂无

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

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