我希望测试嵌套的fetches。到目前为止,我可以测试第一个fetch的结果,但不能访问另一个。我尝试在第一个fetch调用中嵌套下一个fetch调用,但似乎不起作用。第二个SpyOn函数调用创建了一个jasmine错误,说明我不能对同一个函数有多个spy。
茉莉花试验
let response = new Response(
JSON.stringify({
test: "test",
})
);
// creating a fetch spy
spyOn(window, "fetch").and.returnValue(Promise.resolve(response));
// make call to fetch
callFirstFetch().then((result) => {
expect(window.fetch).toHaveBeenCalledWith("/test");
spyOn(window, "fetch").and.returnValue(Promise.resolve(response));
done();
});而嵌套的抓取代码看起来像这样
function firstFetch(() => {
return fetch("/test")
.then(response => {
response.json();
return secondFetch();
})
});
function secondFetch(() => {
return fetch("/test2")
.then(response => {
response.json();
console.log('done');
})
});
firstFetch();发布于 2021-11-25 15:32:12
下面是模拟fetch api调用的方法:
const okResponse = new Response(JSON.stringify({}), { status: 200, statusText: 'OK', });
spyOn(window, 'fetch').and.resolveTo(okResponse);然后像这样测试它:
expect(window.fetch).toHaveBeenCalledWith(requestUrl, options);https://stackoverflow.com/questions/66200776
复制相似问题