admin管理员组文章数量:1289637
I am trying to mock HTTP fetch in jsfiddle. I am not sure what I do wrong that makes the result not to be equal to the mock one.
Here is my sample code: (You can see the logs in the browser console.)
/
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();
I am trying to mock HTTP fetch in jsfiddle. I am not sure what I do wrong that makes the result not to be equal to the mock one.
Here is my sample code: (You can see the logs in the browser console.)
http://jsfiddle/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();
Share
Improve this question
edited May 8, 2019 at 9:56
Maryam Saeidi
asked Aug 29, 2017 at 18:46
Maryam SaeidiMaryam Saeidi
1,5433 gold badges21 silver badges34 bronze badges
1 Answer
Reset to default 7As fatso83 said in here:
Fetch is a different API from XHR. The underlying library of the XHR stubbing, nise, only supports XHR (and so does Sinon). You can check out sinonjs/nise#7 for some tips on how to acplish this.
This code which is written by Mark Middleton also helped me to do the testing: (Sinon to mock a fetch call)
import sinonStubPromise from 'sinon-stub-promise';
import sinon from 'sinon'
sinonStubPromise(sinon)
let stubedFetch = sinon.stub(window, 'fetch') )
window.fetch.returns(Promise.resolve(mockApiResponse()));
function mockApiResponse(body = {}) {
return new window.Response(JSON.stringify(body), {
status: 200,
headers: { 'Content-type': 'application/json' }
});
}
本文标签: javascriptMock HTTP fetch in sinonStack Overflow
版权声明:本文标题:javascript - Mock HTTP fetch in sinon - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741448906a2379371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论