Angular 4单元测试错误" TypeError:无法读取属性' subscribe'未定义"

时间:2017-09-05 19:56:42

标签: angular unit-testing jasmine karma-jasmine

在单元测试Angular 4组件时,我陷入困境并继续出错。

这是我的组成部分:

阶detail.component.ts

@Component({
  selector: 'order-detail',
  templateUrl: 'order-detail.component.html',
  providers: [OrderService]
})
export class OrderDetailComponent implements OnInit {
  private order: Order;

  constructor(
    private orderService: OrderService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.getOrder();
  }

  private getOrder(): void {
    this.route.paramMap
      .map((params: ParamMap) => {
        if (+params.get('id') === 0) {
          this.router.navigateByUrl('/404', {skipLocationChange: true});
        }

        return params;
      })
      .switchMap((params: ParamMap) => this.orderService.getOrder(+params.get('id')))
      .subscribe((order: Order) => this.order = order);
  }
}

这是一项服务:

order.service.ts

@Injectable()
export class OrderService {
  private ordersUrl = `${environment.apiUrl}/orders/`;

  constructor(private http: HttpClient) {}

  getOrder(id: number): Observable<Order> {
    return this.http.get(`${this.ordersUrl}${id}/`)
      .map(response => response as Order);
  }
}

我想在OrderServive上设置间谍,然后用这样的测试进行单元测试:

阶detail.component.spec.ts

describe('Order detail component', () => {
  let fixture: ComponentFixture<OrderDetailComponent>;
  let page: Page;
  let activatedRoute: ActivatedRouteStub;
  const fakeOrder: Order = {
    id: 1,
    title: 'Test 1',
    contractor: {title: 'Contractor 1'} as Contractor,
  } as Order;

  class Page {
    orderSpy: jasmine.Spy;
    routerSpy: jasmine.Spy;
    title: HTMLElement;

    constructor() {
      const orderService = fixture.debugElement.injector.get(OrderService);
      this.orderSpy = spyOn(orderService, 'getOrder').and.returnValue(Observable.of(fakeOrder));
    }
  }

  function createComponent() {
    fixture = TestBed.createComponent(OrderDetailComponent);
    page = new Page();

    fixture.detectChanges();

    return fixture.whenStable().then(() => {
      fixture.detectChanges();
    });
  }

  beforeEach(() => {
    activatedRoute = new ActivatedRouteStub();
  });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [OrderDetailComponent],
      imports: [RouterTestingModule, HttpClientTestingModule],
      providers: [
        OrderService,
        {provide: ActivatedRoute, useValue: activatedRoute},
        {provide: Router, useClass: RouterStub},
      ],
      schemas: [NO_ERRORS_SCHEMA],
    }).compileComponents();
  }));

  describe('when navigate with existing id', () => {
    beforeEach(async(() => {
      activatedRoute.testParamMap = {id: fakeOrder.id};
      createComponent();
    }));

    it('should show title', () => {
      expect(page.orderSpy).toHaveBeenCalledWith(fakeOrder.id);
    });
  });

});

但我一直收到此错误:TypeError: Cannot read property 'subscribe' of undefined

我从Agular文档中获取ActivatedRouteStub并测试结构:https://angular.io/guide/testing

组件代码可以很好地处理来自后端的实际数据,并向我显示订单。

此外,我的代码库包含类似的测试,它在具有类似服务的另一个组件上成功地执行相同的操作,因此该测试是唯一失败的地方。

使用Angular 4.3.6,Karma 1.7.1和Jasmine 2.6.4。

可能出现什么问题以及如何摆脱这个错误?

1 个答案:

答案 0 :(得分:3)

经过一些研究,我实际上发现了导致错误的原因。我删除了

{provide: Router, useClass: RouterStub},

并且错误消失了。如果没有这个存根,测试现在可以正常工作。

不确定是什么问题,但我认为来自docs的RouterStub类缺少某些必要事项的实现。上课是从这里取的:

https://angular.io/guide/testing#test-a-routed-component

https://angular.io/generated/live-examples/testing/app-specs.eplnkr.html