我有以下情况:
js
import fetch from 'node-fetch'
import httpClient from './myClient/httpClient'
export default class{
async init(){
const response = await fetch('some_url')
return httpClient.init(response.payload)
}
}
A_spec.js
import test from 'ava'
import sinon from 'sinon'
import fetch from 'node-fetch'
import httpClient from './myClient/httpClient'
import A from './src/A'
test('a async test', async (t) => {
const instance = new A()
const stubbedHttpInit = sinon.stub(httpClient, 'init')
sinon.stub(fetch).returns(Promise.resolve({payload: 'data'})) //this doesn't work
await instance.init()
t.true(stubbedHttpInit.init.calledWith('data'))
})
我的想法是检查是否已使用获取请求中获得的有效负载调用了 httpClient 的 init 方法。
我的问题是:当我测试 A 的 init 方法时,如何模拟存根返回值的 fetch 依赖项?