【问题标题】:Unit test angular activate route correctly单元测试角度正确激活路线
【发布时间】:2019-06-24 01:53:03
【问题描述】:

我刚刚开始在我的应用程序中实施一些测试,但 ActivatedRoute 有问题

我已经设置了activatedRouteStub,如angular docs所示

import { convertToParamMap, ParamMap, Params } from '@angular/router';
import { ReplaySubject } from 'rxjs';

// An ActivateRoute test double with a `paramMap` observable.
// Use the `setParamMap()` method to add the next `paramMap` value.

export class ActivatedRouteStub {
  // Use a ReplaySubject to share previous values with subscribers
  // and pump new values into the `paramMap` observable
  private subject = new ReplaySubject<ParamMap>();

  constructor(initialParams?: Params) {
    this.setParamMap(initialParams);
  }

  /** The mock paramMap observable */
  readonly paramMap = this.subject.asObservable();

  /** Set the paramMap observables's next value */
  setParamMap(params?: Params) {
    this.subject.next(convertToParamMap(params));
  }

}

然后在我的 spec.ts 组件中

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AuthFormComponent } from './auth-form.component';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRouteStub } from 'testing/activated-route-stub';

describe('AuthFormComponent', () => {
  const routerSpy = jasmine.createSpyObj('Router', ['navigateByUrl']);
  let component: AuthFormComponent;
  let fixture: ComponentFixture<AuthFormComponent>;
  let activatedRoute;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ AuthFormComponent ],
      imports: [
        FormsModule,
        ReactiveFormsModule,
      ],
      providers: [
        { provide: Router, useValue: routerSpy },
        { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AuthFormComponent);
    component = fixture.componentInstance;
    activatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
    activatedRoute.setParamMap({ _ga: 1886587047.1559712233 });
    fixture.detectChanges();
  });

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

基本上,我的表单中有一个函数来检查参数中是否有 _ga 代码,像这样

checkForGACode() {
        this.activatedRoute.queryParams
            .subscribe((result) => {
                if (result._ga) {
                    this._storage.setSession('_ga', result._ga.split('-')[1]);
                }
            });
}

但是当我运行我的测试时,我得到了

不知道我在这里做错了什么,任何帮助将不胜感激!

编辑

在另一篇文章中,我看到提供程序以另一种方式实现

{ provide: ActivatedRoute, useClass: ActivatedRouteStub }

但我只是得到一个不同的错误

【问题讨论】:

  • 您可以导入RouterTestingModule 而不是RouterModule。该模块提供了所有路由器服务的模拟实现。

标签: angular karma-jasmine


【解决方案1】:

您可能可以添加以下代码行,它应该可以工作

 Provide: ActivatedRoute,
 useValue: {
    queryParams: returnAMockObservableHere
 }

顺便说一句,您可以定义一个类并将其替换为UseClass,而不是UseValue

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多