[英]res.json() is undefined when mocking post request with fetch-mock and isomrphic-fetch
I'm using fetch-mock
to test my client action creators in cases where there is an async call being made to the BE. 在对BE进行异步调用的情况下,我正在使用
fetch-mock
测试我的客户端操作创建者。 While all get requests are working well I'm having hard time doing the same to post and put requests. 虽然所有获取请求都运行良好,但我很难这样做来发布和放置请求。
Attached here a code example that if works I believe that my actual code will work as well. 在此附上一个代码示例,如果可以的话,我相信我的实际代码也可以工作。
I'm using import fetchMock from 'fetch-mock'
for mocking the response and require('isomorphic-fetch')
directly to replace the default fetch 我正在使用
import fetchMock from 'fetch-mock'
响应并直接使用require('isomorphic-fetch')
来替换默认的fetch
I added some comments but I do get a response with status 200 (if I change the mocked response status to 400 I get it as well. The problem is that res.json() resulted with undefined instead of the mocked result body. Using JSON.stringify is something that I used after not being able to make it work without it. 我添加了一些注释,但是我确实得到了状态为200的响应(如果我将模拟的响应状态更改为400,我也会得到它。问题是res.json()的结果是undefined而不是模拟的结果主体。使用JSON .stringify是我无法使用它后无法使用的东西。
const responseBody = {response: 'data from the server'};
fetchMock.once('http://test.url', {
status: 200,
body: JSON.stringify(responseBody),
statusText: 'OK',
headers: {'Content-Type': 'application/json'},
sendAsJson: false
}, {method: 'POST'});
fetch('http://test.url',
{
method: 'post',
body: JSON.stringify({data: 'Sent payload'}),
headers : {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then(function (res) {
expect(res.status).toEqual(200); // Pass
res.json();
})
.then(function (json) {
console.log(json); // print undefine
expect(json).toEqual(responseBody); // Fail expected value to equal: {"response": "data from the server"} Received: undefined
done();
})
In your first then, you don't have an explicit return, with the keyword return
在您的第一个操作中,您没有使用关键字
return
的显式return
If you don't do a return, the next then doesn't know the value. 如果您不做回报,那么下一个将不知道其价值。 That's why your json is undefined.
这就是为什么您的json未定义的原因。
For example: 例如:
var myInit = { method: 'GET', mode: 'cors', cache: 'default' };
fetch('https://randomuser.me/api/',myInit)
.then(function(res) {
return res.json()
})
.then(function(r) {
console.log(r)
})
So, for you: 因此,为您:
const responseBody = {response: 'data from the server'};
fetchMock.once('http://test.url', {
status: 200,
body: JSON.stringify(responseBody),
statusText: 'OK',
headers: {'Content-Type': 'application/json'},
sendAsJson: false
}, {method: 'POST'});
fetch('http://test.url',
{
method: 'post',
body: JSON.stringify({data: 'Sent payload'}),
headers : {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then(function (res) {
expect(res.status).toEqual(200); // Pass
return res.json(); // return here
})
.then(function (json) {
console.log(json); // print undefine
expect(json).toEqual(responseBody); // Fail expected value to equal: {"response": "data from the server"} Received: undefined
done();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.