フェッチを使用しているスタブ/テスト機能に問題があります。私はエラーを取得していますスタブフェッチのapiリクエスト
import { expect } from 'chai'
import sinon from 'sinon'
import Promise from 'es6-promise'
Promise.polyfill()
import 'whatwg-fetch'
import clickTimeseries from './above'
const jsonOK = body => {
const mockResponse = new Response(JSON.stringify(body), {
status: 200,
headers: {
'Content-type': 'application/json'
}
})
return Promise.resolve(mockResponse)
}
describe('APIs',() => {
describe('Click timeseries',() => {
it('builds a correct data on success',() => {
sinon.stub(window, 'fetch').returns(jsonOK({stuff: [1,2,3]}))
expect(clickTimeseries()).to.eq({
success: true,
data: {stuff: [1,2,3]}
})
})
})
})
:
expected { Object (, _state, ...) } to equal { success: true, data: {stuff: [ 1, 2, 3, 4 ] }}
それはspendTimeseries
返す約束、代わりの結果のように見える
export const clickTimeseries => {
return fetch('...url...')
.then(response => {
if(response.status == 200)
return response.json()
else
throw 'something'
})
.then(json => {
return {
success: true,
data: json,
}
})
.catch(err => {
return {
success: false,
error: err,
}
})
}
そして、私のテスト:簡単な例を使用して
両方ともthen
ブロックを呼び出します。
テストをパスするにはどうすればよいですか?
感謝:ちょうどこのように読むためのテストを変更!仲間にスポット。 – Kocur4d