【问题标题】:routerLinkActive not working in unit testingrouterLinkActive 在单元测试中不起作用
【发布时间】:2020-03-31 14:51:48
【问题描述】:

RouterLinkActive 在单元测试期间不起作用,但在单元测试外部时起作用。

这里是 HTML:

<header class="site-tab">
  <nav class="c-tab l-wrap">
    <div
      class="c-tab__item"
      routerLink="/companies"
      routerLinkActive="c-tab__item--active"
    >
      <p class="c-tab__text">Companies</p>
    </div>
    <div
      class="c-tab__item"
      routerLink="/products"
      routerLinkActive="c-tab__item--active"
    >
      <p class="c-tab__text">Products</p>
    </div>
    <div
      class="c-tab__item"
      routerLink="/invoices"
      routerLinkActive="c-tab__item--active"
    >
      <p class="c-tab__text">Invoices</p>
    </div>
  </nav>
</header>

这是我的规范文件:

import { ComponentFixture, TestBed, async } from '@angular/core/testing';

import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { TabsComponent } from './tabs.component';

describe('TabsComponent', () => {
  let component: TabsComponent;
  let fixture: ComponentFixture<TabsComponent>;
  let de: DebugElement;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [TabsComponent],
      imports: [RouterTestingModule]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TabsComponent);
    component = fixture.componentInstance;
    de = fixture.debugElement;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should set clicked tab to active', () => {
    de.queryAll(By.css('.c-tab__item'))[1].nativeElement.click();
    expect(
      de.query(By.css('.c-tab__item--active .c-tab__text')).nativeElement
        .innerHTML
    ).toContain('Product');
  });

我希望一旦单击该选项卡就会处于活动状态,但出现此错误 TypeError: de.query(...) is null in http://localhost:9876/_karma_webpack_/main.js (line 872) 我相信这意味着没有标签被设置为活动状态。

【问题讨论】:

  • 为什么要用debug element,然后调用native element? fixture.nativeElement 应该可以工作。
  • 我假设你说我的点击功能不起作用?您能否详细说明如何从单元测试中单击选项卡

标签: angular jasmine karma-jasmine


【解决方案1】:

我也面临同样的问题,即使在阅读此问题时也是如此。 但是我在应用fakeAsync函数和tick()后得到了解决方案。

     it('should set clicked tab to active', fakeAsync(() => {
        de.queryAll(By.css('.c-tab__item'))[1].nativeElement.click();
        fixture.detectChanges(); // add this
        tick(); // add this too
        expect(
          de.query(By.css('.c-tab__item--active .c-tab__text')).nativeElement
            .innerHTML
        ).toContain('Product');
      }));

【讨论】:

    【解决方案2】:

    我不确定clicking 是否会发挥router 的魔力,但它可能会。但我相信你在点击后错过了fixture.detectChanges()

    试试:

      it('should set clicked tab to active', () => {
        de.queryAll(By.css('.c-tab__item'))[1].nativeElement.click();
        fixture.detectChanges(); // add this.
        expect(
          de.query(By.css('.c-tab__item--active .c-tab__text')).nativeElement
            .innerHTML
        ).toContain('Product');
      });
    

    【讨论】:

    • 我可以使用 router.navigate 做到这一点,但这似乎也可以正常工作。谢谢。
    • 仍然无法在 spec.ts 的选择器中获取活动类
    猜你喜欢
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多