角度2 - 测试路线参数的变化

Stu*_*Stu 15 jasmine angular2-routing angular2-testing angular2-observables angular

我有一个角度为2的组件,它响应路由参数的变化(组件不会从头开始重新加载,因为我们没有移出主路径.这是组件代码:

export class MyComponent{
    ngOnInit() {
        this._routeInfo.params.forEach((params: Params) => {
            if (params['area']){
                this._pageToShow =params['area'];
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一种享受,并_pageToShow适合导航.

我正在尝试测试路线变化的行为(所以这是可观察的第二个触发器,但它拒绝为我工作.)这是我的尝试:

it('sets PageToShow to new area if params.area is changed', fakeAsync(() => {
    let routes : Params[] = [{ 'area': "Terry" }];
    TestBed.overrideComponent(MyComponent, {
        set: {
            providers: [{ provide: ActivatedRoute,
                useValue: { 'params': Observable.from(routes)}}]
        }
    });

    let fixture = TestBed.createComponent(MyComponent);
    let comp = fixture.componentInstance;
    let route: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
    comp.ngOnInit();

    expect(comp.PageToShow).toBe("Terry");
    routes.splice(2,0,{ 'area': "Billy" });

    fixture.detectChanges();
    expect(comp.PageToShow).toBe("Billy");
}));
Run Code Online (Sandbox Code Playgroud)

但是TypeError: Cannot read property 'subscribe' of undefined当我运行它时会引发异常.如果我在没有fixture.detectChanges();线路的情况下运行它会失败,因为第二个期望失败.

Pau*_*tha 26

首先,你应该使用a Subject而不是a Observable.observable只能订阅一次.所以它只会发出第一组参数.使用a Subject,您可以继续发出项目,单个订阅将继续获取它们.

let params: Subject<Params>;

beforeEach(() => {
  params = new Subject<Params>();
  TestBed.configureTestingModule({
    providers: [
      { provide: ActivatedRoute, useValue: { params: params }}
    ]
  })
})
Run Code Online (Sandbox Code Playgroud)

然后在你的测试中只发出新的值params.next(newValue).

其次,你需要确保打电话tick().这是怎么回事fakeAsync.您可以控制异步任务解析.由于可观察为asychrounous,在我们发送事件的那一刻,它不会同步到达用户.所以我们需要强制同步行为tick()

这是一个完整的测试(Subject从中导入'rxjs/Subject')

@Component({
  selector: 'test',
  template: `
  `
})
export class TestComponent implements OnInit {

  _pageToShow: string;

  constructor(private _route: ActivatedRoute) {
  }

  ngOnInit() {
    this._route.params.forEach((params: Params) => {
      if (params['area']) {
        this._pageToShow = params['area'];
      }
    });
  }
}

describe('TestComponent', () => {
  let fixture: ComponentFixture<TestComponent>;
  let component: TestComponent;
  let params: Subject<Params>;

  beforeEach(() => {
    params = new Subject<Params>();
    TestBed.configureTestingModule({
      declarations: [ TestComponent ],
      providers: [
        { provide: ActivatedRoute, useValue: { params: params } }
      ]
    });
    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
  });

  it('should change on route param change', fakeAsync(() => {
    // this calls ngOnInit and we subscribe
    fixture.detectChanges();

    params.next({ 'area': 'Terry' });

    // tick to make sure the async observable resolves
    tick();

    expect(component._pageToShow).toBe('Terry');

    params.next({ 'area': 'Billy' });
    tick();

    expect(component._pageToShow).toBe('Billy');
  }));
});
Run Code Online (Sandbox Code Playgroud)

  • 确保在此解决方案中向下滚动以包含在`tick()`函数中!我第一次完全错过了.谢谢你的解决方案,这对我有用:) (2认同)

Vic*_*hin 5

我更喜欢像这样从ActivatedRouteSnapshot获取路由参数和数据 this.route.snapshot.params['type']

如果使用相同的方式,则可以像这样测试

1)在您的测试提供者中

{provide: ActivatedRoute, useValue: {snapshot: { params: { type: '' } }}}
Run Code Online (Sandbox Code Playgroud)

2)在您的测试规格中

it('should...', () => {
   component.route.snapshot.params['type'] = 'test';
   fixture.detectChanges();
   // ...
});
Run Code Online (Sandbox Code Playgroud)