角度服务测试:找不到名称"asyncData" [英] Angular service testing: Cannot find name 'asyncData'

查看:118
本文介绍了角度服务测试:找不到名称"asyncData"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学习如何在Angular中测试服务,我试图将以下示例复制到Angular文档中.

So I'm learning how to test services in Angular and I tried to copy the below example in the Angular docs.

let httpClientSpy: { get: jasmine.Spy };
let heroService: HeroService;

beforeEach(() => {
  // TODO: spy on other methods too
  httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
  heroService = new HeroService(<any> httpClientSpy);
});

it('should return expected heroes (HttpClient called once)', () => {
  const expectedHeroes: Hero[] =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));

  heroService.getHeroes().subscribe(
    heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
    fail
  );
  expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});

我试图从字面上复制它,但是它给了我以下错误:

I tried to copy it quite literally, but it gives me the following error:

src/app/services/find-locals.service.spec.ts(17,38)中的

ERROR:错误TS2304:找不到名称"asyncData".

ERROR in src/app/services/find-locals.service.spec.ts(17,38): error TS2304: Cannot find name 'asyncData'.

有人可以代替我吗?还是告诉我在其他地方可能做错了什么?

这是从Angular文档复制的测试文件:

import {FindLocalsService} from './find-locals.service';

import {HttpClient, HttpClientModule} from '@angular/common/http';

let findLocalsService: FindLocalsService;
let httpClientSpy: { get: jasmine.Spy, post: jasmine.Spy };

beforeEach(() => {
  httpClientSpy = jasmine.createSpyObj('HttpClient', ['get', 'post']);
  findLocalsService = new FindLocalsService(<any> httpClientSpy, null);
});

it('should save location to server', function () {
  const expectedData: any =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.post.and.returnValue(asyncData(expectedData));

  findLocalsService.saveLocation('something').subscribe(
    data => expect(data).toEqual(expectedData),
          fail
  );

  expect(httpClientSpy.post.calls.count()).toBe(1, 'one call');
});

这是服务本身

@Injectable()

export class FindLocalsService {

    constructor(private http: HttpClient, private authService: AuthenticationService){}

    saveLocation(locationObj){
        return this.http.post(url + '/findLocals/saveLocation', locationObj);
    }

    getThreeClosestPlayers() {
        const userId = this.authService.currentUser().user._id;
        console.log('entered 3 closest service', userId);

        return this.http.get(url + '/findLocals/getThreeClosestPlayers/' + userId)
            .pipe(
              map((data: any) => data.obj),
              catchError(this.handleError)
              )
    }
}

推荐答案

更改此行:

httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));

使用Observable运算符 of()

to use the Observable operator of()

httpClientSpy.get.and.returnValue(of(expectedHeroes));

这将返回一个可订阅的Observable,并将返回ExpectedHeroes.如果您使用的是Angular 6,则可以直接从 rxjs 导入:

This will return an observable that can be subscribed to and will return expectedHeroes. If you are using Angular 6, you can import this directly from rxjs:

import {of} from 'rxjs';

这篇关于角度服务测试:找不到名称"asyncData"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆