0
我有一個問題,使用提取的存根/測試功能。你如何存根提取API請求
使用簡單的例子:
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,
}
})
}
而且我的測試:
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
回報的承諾,呼叫then
塊。
您將如何設置測試才能通過測試?
感謝您的時間!現貨在隊友。 – Kocur4d