typescript Angular 单元测试 TypeError: this.http.get(...).pipe is not a function
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50948524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular unit tests TypeError: this.http.get(...).pipe is not a function
提问by med.b
I'm following this example to make a unit test for service (get request coming from spring app backend) https://angular.io/guide/testing#testing-http-services
我正在按照此示例对服务进行单元测试(从 spring 应用程序后端获取请求)https://angular.io/guide/testing#testing-http-services
Service class :
服务等级:
@Injectable()
export class TarifService {
constructor(private messageService: MessageService, private http: HttpClient) { }
public getTarifs(): Observable<Tarif[]> {
return this.http.get<Tarif[]>(tarifffsURL).pipe(
tap(() => {}),
catchError(this.handleError('getTarifs', []))
);
}
}
Unit Test
单元测试
describe('TarifService', () => {
let tarifService: TarifService;
let httpClientSpy: { get: jasmine.Spy };
let expectedTarifs;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ToastrModule.forRoot(), HttpClientModule, HttpClientTestingModule],
providers: [TarifService, HttpClient, MessageService]
});
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
tarifService = new TarifService(<any> MessageService,<any> httpClientSpy);
});
it('should be created', inject([TarifService], (service: TarifService) => {
expect(service).toBeTruthy();
}));
it('should return expected tarif (HttpClient called once)', () => {
const expectedTarifs: Tarif[] =
[{ id: 1, name: 'Tarif1', value: '20' }, { id: 2, name: 'Tarif2', value:'30' }];
httpClientSpy.get.and.returnValue(expectedTarifs);
tarifService.getTarifs().subscribe(
tarifs => expect(tarifs).toEqual(expectedTarifs, 'expected tarifs'),
fail
);
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
});
When running the tests, I keep having this error
运行测试时,我一直有这个错误
TarifService should return expected tarif (HttpClient called once)
TypeError: this.http.get(...).pipe is not a function
what could be the cause of this?
这可能是什么原因?
回答by Castro Roy
The problem is that when your spyis being called, it is returning an Arrayand Arraydoes not have a pipefunction. You need to return an Observablefrom your spy, something like this
问题是当你spy被调用时,它返回一个Array并且Array没有pipe函数。你需要Observable从你的返回一个spy,像这样
const expectedTarifs: Tarif[] =
[{ id: 1, name: 'Tarif1', value: '20' }, { id: 2, name: 'Tarif2', value:'30' }];
httpClientSpy.get.and.returnValue(Observable.of(expectedTarifs));
See how the returnValueis Observable.of(expectedTarifs). Observable.ofcreates an Observablethat emits some values you specify as arguments, immediately one after the other, and then emits a complete notification. See the docs
看怎么returnValue是Observable.of(expectedTarifs)。Observable.of创建一个Observable发出您指定为参数的一些值,立即一个接一个,然后发出完整的通知。查看文档
Hope it helps
希望能帮助到你
回答by zappa
I had to do this to get mine to work:
我必须这样做才能让我的工作:
import { from } from 'rxjs';
then:
然后:
return from([expectedTarifs]);

