Angular 我在测试RxJs时遇到问题-(TypeError:Cannotreadproperty';length';of undefined)
我有一个http请求,并对响应进行了一些修改。我做不出正确的测试,请帮帮我。我在“tap”操作员分支中总是有问题。响应是一个数组 以下是我的简化服务:Angular 我在测试RxJs时遇到问题-(TypeError:Cannotreadproperty';length';of undefined),angular,http,testing,rxjs,tap,Angular,Http,Testing,Rxjs,Tap,我有一个http请求,并对响应进行了一些修改。我做不出正确的测试,请帮帮我。我在“tap”操作员分支中总是有问题。响应是一个数组 以下是我的简化服务: getRequest() { const otherIds = [] return this.http .get<any>(url) .pipe( map(response => { // array return response
getRequest() {
const otherIds = []
return this.http
.get<any>(url)
.pipe(
map(response => { // array
return response
.map(id=> {
return id
})
.sort((a, b) => {
debugger
return a > b ? 1 : -1
})
}),
tap(response => {
response.forEach(element => {
otherIds.push(element.id)
})
})
)
}
it('getRequest: should get request', getRequest)
function getRequest() {
let responseIds: any
let otherIds = []
service.getRequest().subscribe(data => {
responseIds= data
data.forEach(id => otherIds.push(id))
expect(otherIds.length).toBeGreaterThan(0)
}
httpMock
.expectOne({ url, method: 'GET' })
.flush(idMock) // idMock is an array with 3 items
expect(responseIds.length).toBe(3)
}