angular - HttpTestingController ExpectOne匹配URL错误

标签 angular jestjs

我正在测试我的http服务类并尝试测试请求,但是我这个错误

Expected one matching request for criteria "Match URL: https://beneficios/api/v1/processobeneficio/1111111111111/timeline", found none.

我不知道为什么,我在服务和测试类上使用相同的参数。

服务等级
export class AcompanhamentoStatusService {

  constructor(private http: HttpClient) { }

  public ObterStatusResgate<T>(protocolo: string): Observable<T> {

    return this.http.get<T>(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
  }

测试
describe('AcompanhamentoStatusService', () => {
  let service: AcompanhamentoStatusService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [ AcompanhamentoStatusService ],
      imports: [ HttpClientTestingModule ],
    });
    service = TestBed.get(AcompanhamentoStatusService);
    httpMock = TestBed.get(HttpTestingController);

  });

  it('should have apiEndpoint in Enviroment', () => {
    expect(environment.apiEndpoint).toBeDefined();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
    expect(service).toBeDefined();
  });

  it('Deve retornar status acompanhamento', () => {
    const protocolo = '1111111111111';
    const mockPassos = [
      { status: 'Em Análise', data: '20/08/2018'},
      { status: 'Pago', data: '25/08/2018'},
    ];

    service.ObterStatusResgate(protocolo).subscribe( passos => {
      expect(passos).toBe(mockPassos);
    });

    const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
    expect(req.request.method).toBe('GET');
    req.flush(mockPassos);
  });

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

所有测试都通过了,只有一个失败了,不知道为什么会这样吗?

最佳答案

我遇到了同样的问题,我在项目中用注释了类似于您的代码的三行代码,并得到了我的代码使用的真实URL,因此:

const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
expect(req.request.method).toBe('GET');
req.flush(mockPassos);

然后,我得到了当前的 URL ,我知道这是不匹配的。

关于angular - HttpTestingController ExpectOne匹配URL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408914/

相关文章:

javascript - Summernote 自定义按钮传递上​​下文和数据

angular - 在 Angular 中离线播放 Azure 媒体服务中的视频

javascript - 如何实例化要在单元测试中使用的 Sequelize 模型?

javascript - Vue jest 不等待 axios 发布

javascript - React Native Jest 测试 : TypeError: Cannot read property 'unsubscribeFromTopic' of undefined

javascript - JestJS:如何测试嵌套(异步)函数中的调用函数

node.js - 在git root子目录中的Docker容器中以--watch模式运行Jest

angular - Nativescript + Angular ScrollView 滚动到结束监听器

angular - 类型 { production : boolean; } 上不存在属性 'firebase'

javascript - Angular 2 AOT 与 JIT 负载比较