【问题标题】:Testing Angular 2 parent route测试 Angular 2 父路由
【发布时间】:2016-12-03 17:32:00
【问题描述】:

我有一个像这样访问父路由值的组件:

  ngOnInit() {
    this.route.parent.parent.params.subscribe(params => {
      this.placeService.getPlace(params['id']).subscribe(place => this.place = place);
    });
  }

这是完整的文件,以防万一。

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { PlaceService } from '../place.service';
import { Place } from '../place';

@Component({
  selector: 'app-diner-review-list',
  templateUrl: './diner-review-list.component.html',
  styleUrls: ['./diner-review-list.component.css'],
  providers: [PlaceService]
})
export class DinerReviewListComponent implements OnInit {
  place: Place;

  constructor(private route: ActivatedRoute, private placeService: PlaceService) { }

  ngOnInit() {
    this.route.parent.parent.params.subscribe(params => {
      this.placeService.getPlace(params['id']).subscribe(place => this.place = place);
    });
  }
}

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

TypeError: 无法读取 null 的属性“父级”

这是有道理的。 this.route.parent null。但我不在乎。我现在不测试路线。

这是我的测试:

/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { MockPlaceService } from '../../../testing/mock-place.service';

import { DinerReviewListComponent } from './diner-review-list.component';
import { PlaceService } from '../place.service';

let mockPlaceService = new MockPlaceService();

describe('DinerReviewListComponent', () => {
  let component: DinerReviewListComponent;
  let fixture: ComponentFixture<DinerReviewListComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        DinerReviewListComponent
      ],
      imports: [RouterTestingModule]
    })
    .overrideComponent(DinerReviewListComponent, {
      set: {
        providers: [
          { provide: PlaceService, useValue: mockPlaceService }
        ]
      }
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DinerReviewListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

我怎样才能让我的测试忽略缺席的路线值或提供虚假的路线值?

【问题讨论】:

    标签: angular routing


    【解决方案1】:

    我最终以后一种方式解决了它:提供假路线。这就是我所做的。

    注意提到mockActivatedRoute的部分。

    /* tslint:disable:no-unused-variable */
    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    import { RouterTestingModule } from '@angular/router/testing';
    import { ActivatedRoute } from '@angular/router';
    import { By } from '@angular/platform-browser';
    import { DebugElement } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/of';
    
    import { MockPlaceService } from '../../../testing/mock-place.service';
    
    import { DinerReviewListComponent } from './diner-review-list.component';
    import { PlaceService } from '../place.service';
    
    let mockPlaceService = new MockPlaceService();
    
    class MockActivatedRoute {
      parent: any;
      params: any;
    
      constructor(options) {
        this.parent = options.parent;
        this.params = options.params;
      }
    }
    
    let mockActivatedRoute = new MockActivatedRoute({
      parent: new MockActivatedRoute({
        parent: new MockActivatedRoute({
          params: Observable.of({ id: '1' })
        })
      })
    });
    
    describe('DinerReviewListComponent', () => {
      let component: DinerReviewListComponent;
      let fixture: ComponentFixture<DinerReviewListComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [
            DinerReviewListComponent
          ],
          imports: [RouterTestingModule]
        })
        .overrideComponent(DinerReviewListComponent, {
          set: {
            providers: [
              { provide: PlaceService, useValue: mockPlaceService },
              { provide: ActivatedRoute, useValue: mockActivatedRoute }
            ]
          }
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(DinerReviewListComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    【讨论】:

    • 更短的选择:{ provide: ActivatedRoute, useValue: {parent: {parent: {params; {subscribe: function() { return; }}}}} - 谢谢@Jason!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多