Ошибка: Ожидается один соответствующий запрос по критериям «Соответствующий URL: api / xyz», не найден - PullRequest
0 голосов
/ 10 октября 2019

Я пишу юнит-тест, который проверяет, вызывается API или нет. Чтобы добраться до функции, которая вызывает API, я должен заполнить значения, которые я получу от службы.

Вот фрагмент кода

component.ts

ngOnInit() {

  this.subscription = this.topToolBarService.change.subscribe(customer => {
    this.filter.customerNumber = customer.customerNumber;
    this.filter.customerName = customer.customerName;
    this.customerid = customer.customerNumber;
    this.getData();// here (1)
 });

  let customer = this.topToolBarService.getCustomer();
    if (customer == null) {
     //code gets blocked here
    } else {
      this.filter.customerNumber = customer.customerNumber;
      this.filter.customerName = customer.customerName;
      this.customerid = customer.customerNumber;
      this.getData();   //here (2) 
    }
}

 getData() {
  this.http.post...
   .....calls api
 }

service.ts

import { EventEmitter, Injectable, Output } from "@angular/core";

@Injectable()
export class TopToolBarService {

  customer = null;

  @Output() change: EventEmitter<string> = new EventEmitter();

   getCustomer() {
    return this.customer;
  }

}

component.spec.ts

it('should fetch all the activated accounts from cosmos using /api/users', () => {

    spyOn(component, 'getData').and.returnValue(of(response));  
    //attempt to somehow get passed through subscribe  
    fixture.detectChanges();
    component.filter.customerName = "123";
    fixture.detectChanges();
    component.filter.customerNumber = "xyz";
    fixture.detectChanges(); 

    const req = _HttpTestingController.expectOne('api/xyz');//test fails here
  Error: Expected one matching request for criteria "Match URL: api/xyz", found none.
    expect(req.request.method).toBe("POST");
    let responseData = component.records;
    req.flush({status:"SUCCESS","data":responseData});

  }) 

Мне придется как-то высмеивать изменения. подпишитесь и вызовите функцию, которая вызывается в этом блоке кода, или заставьте сервисный метод возвращать действительного клиента, отличного от нуля, для вызова функции.

Как мне сделать тестовый проход, любая идея?

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...