Ожидается один соответствующий запрос по критериям - PullRequest
0 голосов
/ 28 июня 2018

Я пытаюсь следовать угловому руководству по тестированию сервисов с использованием нового HTTP-клиента. Я получаю следующую ошибку: Expected one matching request for criteria "Match method: GET, URL: http://localhost:8080/services/shift/2016-12-01", found none. Я поместил свой код ниже, не очень уверен, где я иду не так

Модульный тест

import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClient, HttpHandler } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';

import { ShiftService } from './shift.service';

let service: ShiftService;
let backend: HttpTestingController;

describe('ShiftService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        ShiftService,
        HttpClient,
        HttpHandler
      ],
      imports: [HttpClientTestingModule]
    });

    service = TestBed.get(ShiftService);
    backend = TestBed.get(HttpTestingController);
  });

  afterEach(() => {
    backend.verify();
  });

  describe('When the getShift method is invoked', () => {
    it('should make a GET request to the services/shift endpoint', async() => {
      service.getShift().subscribe();
      backend.expectOne({
        url: 'http://localhost:8080/services/shift/2016-12-01',
        method: 'GET'
      });
    });
  });
});

Услуги

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})

export class ShiftService {
  constructor(private http: HttpClient) { }

  public getShift = () => {
    return this.http.get('http://localhost:8080/services/shift/2016-12-01');
  }
}

Я подписался на мой метод getShift () и использую HTTPTestingController. Я также попробовал другие перегрузки HttpTestingController и не повезло: / Заранее благодарен за любую помощь!

1 Ответ

0 голосов
/ 19 июля 2019
  use describe as below

 describe('When the getShift method is invoked', () => {
    it('should make a GET request to the services/shift endpoint', async() => {
        const path = '/testPath';
        service.get(path).subscribe(response => {
            expect(response).toBeTruthy();
        });

        const httpRequest = httpMock.expectOne(
        (req: HttpRequest<any>) => req.urlWithParams === path); 

     // write your expect criteria here....
      });
  });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...