簡體   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