Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法将请求url与HttpTestingController匹配_Angular_Api_Unit Testing - Fatal编程技术网

Angular 无法将请求url与HttpTestingController匹配

Angular 无法将请求url与HttpTestingController匹配,angular,api,unit-testing,Angular,Api,Unit Testing,我刚开始用Angular编写单元测试。首先,我想为我的服务编写测试,以从外部服务器获取数据。我试图检查方法的类型。我使用了一些教程,但我无法连接到服务器。下面是我的代码 import { GitHubService} from './github.service'; import { TestBed, inject } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController

我刚开始用Angular编写单元测试。首先,我想为我的服务编写测试,以从外部服务器获取数据。我试图检查方法的类型。我使用了一些教程,但我无法连接到服务器。下面是我的代码

import { GitHubService} from './github.service';
import { TestBed, inject } from '@angular/core/testing';
import {
  HttpClientTestingModule,
  HttpTestingController
} from '@angular/common/http/testing';

describe('GitHubService', () => {
  let service: GitHubService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [GitHubService]
    });

    service = TestBed.get(GitHubService);
    httpMock = TestBed.get(HttpTestingController);
  });

  it('should create GitHubService', inject(
    [GitHubService],
    (service: GitHubService) => {
      expect(service).toBeTruthy();
    }
  ));

  it('service url should be a GET method', () => {
    const req = httpMock.expectOne(`${service.API_URL}/users`);
    expect(req.request.method).toBe('GET');
  });

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


运行
ng测试后
结果为
1失败,1成功
和提交的错误:
error:应为“匹配URL:https://api.github.com/users,未找到任何文件。
。我一直在网上寻找答案,但解决方案不起作用。我的代码有什么问题,我应该纠正什么?感谢您的建议。

您从未在任何地方调用您服务的任何方法(并订阅它返回的可观察方法),因此它从未发送任何请求,因此测试失败,因为模拟后端确实从未收到任何东西。非常感谢。我想指出,如果您使用
HTTP\u拦截器
,您还应该在测试文件中提供它