gpt4 book ai didi

node.js - Stubbing Fetch Call - response.json 不会调用

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:29 25 4
gpt4 key购买 nike

我正在尝试用 sinon 和 sinon-stub-promise stub 获取调用。我非常接近......但我不确定为什么它不调用我创建的 response.json() 方法。

function toJSON(response) {
console.log(response);
return response.json();
}

function init() {
fetch(darkWeatherUrl)
.then(toJSON)
.then(computeHours)
.then(sendAlerts)
.catch((e) => {
console.log('init error ' + e);
});
}

describe('lifx alert test', ()=> {

it('should run fetch', ()=> {

var fetch = sinon.stub().returnsPromise();

var body = {
"hourly": {
data:
[ { time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17 },
{ time: 1493416800,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.89 },
{ time: 1493420400,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.73 },
{ time: 1493424000,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.31 } ]
}
};


function json() {
return body;
}

var response = {};

response.json = json;


fetch.resolves(response);
var init = proxy('../index.js', {'node-fetch': fetch});
init();
fetch.should.have.been.called;
});

});

 lifex alert test
{ json: [Function: json] }
[ { time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17 },
{ time: 1493416800,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.89 },
{ time: 1493420400,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.73 },
{ time: 1493424000,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.31 } ]
init error TypeError: response.json is not a function
â should run fetch


1 passing

最佳答案

另请参阅底部的更新

我删除了 sinon-stub-promise 并将其保留为原生 sinon:

describe('lifx alert test', ()=> {
var fetchStub;

beforeEach(() => {
fetchStub = sinon.stub();
});

it('should run fetch', ()=> {

var body = {
"hourly": {
data:
[ { time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17 },
{ time: 1493416800,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.89 },
{ time: 1493420400,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.73 },
{ time: 1493424000,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.31 } ]
}
};


var response = { json: () => { return body } };
fetchStub.returns(Promise.resolve(response));
var init = proxy('../index.js', {'node-fetch': fetchStub});
init();
fetchStub.should.have.been.called;
});

更新

我让它与具有 proxyquire 全局属性的 sinon-stub-promise 一起工作:

describe('lifx alert test', ()=> {

it('should run fetch', ()=> {

var fetch = sinon.stub().returnsPromise();

var body = {
"hourly": {
data:
[ { time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17 },
{ time: 1493416800,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.89 },
{ time: 1493420400,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 296.73 },
{ time: 1493424000,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 50,
ozone: 296.31 } ]
}
};


var response = { json: () => { return body } };


fetch['@global'] = true;
fetch.resolves(response);
proxy('../index.js', {'node-fetch': fetch});
fetch.should.have.callCount(3);
});

});

关于node.js - Stubbing Fetch Call - response.json 不会调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com