使用角度CLI进行单元测试,HTTP测试不起作用

时间:2020-03-22 11:17:58

标签: angular testing

albums.spec.ts

describe("AlbumsComponent", () => {
  let component: AlbumsComponent;
  let fixture: ComponentFixture<AlbumsComponent>;
  let albumService: AlbumService;
  let httpMock: HttpTestingController;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
       ......
      ],
      imports: [AppRoutingModule, HttpClientTestingModule],
      providers: [AlbumService, HttpTestingController]
    }).compileComponents();
    albumService = TestBed.get(AlbumService);
    httpMock = TestBed.get(HttpTestingController);
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AlbumsComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it("should retrieve posts from the API via GET", () => {
    const dummyAlbums: Album[] = [
      {
        userId: 1,
        userName: "Arpan",
        id: 1,
        title: "quidem molestiae enim"
      },
      {
        userId: 1,
        userName: "Banerjee",
        id: 2,
        title: "sunt qui excepturi placeat culpa"
      }
    ];
    albumService.fetchAlbums().subscribe(albums => {
      expect(albums.length).toBe(2);
      expect(albums).toEqual(dummyAlbums);
    });
      const request = httpMock.expectOne(
      "http://jsonplaceholder.typicode.com/albums"
    );
    expect(request.request.method).toBe("GET");
    request.flush(dummyAlbums);
  });
});

我遇到了以下错误-

10 specs, 1 failure, randomized with seed 94637
Spec List | Failures
AlbumsComponent > should retrieve posts from the API via GET
**TypeError: httpMock.expectOne is not a function**
    at <Jasmine>
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/albums/albums.component.spec.ts:67:30)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
    at <Jasmine>

没有编译错误。 这是我的服务班

album.service.ts


@Injectable({ providedIn: "root" })
export class AlbumService {
  constructor(private http: HttpClient) {}
  albumUrl = "http://jsonplaceholder.typicode.com/albums";


  //get the album title along with the user name
  **fetchAlbums(): Observable<any> {
    return this.http.get<Album[]>(this.albumUrl).pipe(
      tap(albums => {
        albums.map((album: { userId: String; userName: String }) => {
          this.fetchUsers(album.userId).subscribe((user: any) => {
            album.userName = user[0].username;
          });
        });
        // console.log(albums);
      })
    );
  }**


TypeError:httpMock.expectOne不是函数

我无法理解为什么ExpectOne不是函数。 我是新手,我正在学习角度测试。这是我https://www.youtube.com/watch?v=4JVnSkR04tM&t=376s关注的youtube视频的链接

0 个答案:

没有答案