我试图在jsfiddle中模拟HTTP fetch。我不确定我的错误是什么,导致结果与模拟结果不相等。
这是我的示例代码:(您可以在浏览器控制台中查看日志。)
链接:http://jsfiddle.net/maryam_saeidi/yredb06m/7/
这是我的示例代码:(您可以在浏览器控制台中查看日志。)
链接:http://jsfiddle.net/maryam_saeidi/yredb06m/7/
async function getUser(userId) {
var user = await fetch("http://website/api/users/" + userId);
return user.json();
}
mocha.setup("bdd");
chai.should();
var assert = chai.assert,
expect = chai.expect;
describe('getUser()', () => {
let server;
beforeEach(function() {
server = sinon.fakeServer.create();
});
afterEach(function () {
server.restore();
});
it('should return a user.', async () => {
const response = await getUser(1);
console.log("response:", response);
});
it('should return a user object', async () => {
const userId = 10;
server.respondWith("GET", "http://website/api/users/" + userId,[200, { "Content-Type": "application/json" },
'{ "id": "1", "username": "John", "avatar_url": "A_URL" }']);
const response = getUser(userId);
server.respond();
response.then(function(result){
console.log("result:",result); //The code doesn't get here
result.should.deep.equal({ "id": "1", "username": "John", "avatar_url": "A_URL" });
});
});
});
mocha.run();
sinon.stub()
上无法跨越这个错误:TypeError: Cannot stub non-existent own property fetch
。任何想法我可能做错了什么吗? - carpiediemstubedFetch
没有被任何地方使用。 - montrealist