【发布时间】:2021-02-27 11:39:34
【问题描述】:
下面可以在https://repl.it/@SandraSchlichti/jest-playground-3#getStatusCode.test.js现场试用
最初下面的测试有status: 200,但我尝试将其更改为301,看看我是否理解它是如何工作的。
describe('when the server returns 200', () => {
let result;
//??? The desired purpose of this test is to have a mis-match between expected and
//??? returned status code so getStatusCode() returns 0.
//??? Trying to figure out how this test works.
//??? Here I have changed status to 301 and expected the test to fail, but it didn't
//??? From my understanding mockResponseData contains status 301, and status below
//??? should override that? But that doesn't seam to be the case for some reason.
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 301,
data: mockResponseData
});
result = await getStatusCode();
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
问题
当我将其更改为 status: 301 时,我原以为测试会失败,因为当状态码匹配时,getStatusCode() 应该返回 1。
- 为什么测试没有失败?
- 是否可以修改测试以使其更清楚预期和返回的状态代码不匹配?
- 出于某种原因,
status和data的顺序并不重要。这是为什么?status: 301不应该覆盖mockResponseData内的status吗? - 如果我在
mockResponseData.json中将status更改为9999,则所有测试都不受影响。感觉mockResponseData.json里面的内容根本没用?
这些是本地试用所需的文件:
mockedResponseData.json
{
"status": 301,
"statusText": "Moved Permanently",
"headers": {
"location": "https: //www.google.com/",
"content-type": "text/html; charset=UTF-8",
"date": "Thu, 12 Nov 2020 10: 09: 41 GMT",
"expires": "Sat, 12 Dec 2020 10: 09: 41 GMT",
"cache-control": "public, max-age=2592000",
"server": "gws",
"content-length": "220",
"x-xss-protection": "0",
"x-frame-options": "SAMEORIGIN",
"alt-svc": "h3-Q050=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"",
"connection": "close"
}
}
getStatusCode.test.js
const axios = require('axios');
const getStatusCode = require('./getStatusCode');
const mockResponseData = require('./mockResponseData.json');
jest.mock("axios");
describe("getStatusCode ", () => {
describe('when the server returns status code matching options.statusCode', () => {
let result;
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 200,
data: mockResponseData
})
result = await getStatusCode({
statusCode: 200
})
});
it('should return 1', () => {
expect(result).toEqual(1)
})
});
describe('when the server returns 200', () => {
let result;
//??? The desired purpose of this test is to have a mis-match between expected and returned status code,
//??? getStatusCode() returns 0.
//??? Trying to figure out how this test works.
//??? Here I have changed status to 301 and expected the test to fail, but it didn't
//??? From my understanding mockResponseData contains status 301, and status below should override that?
//??? But that doesn't seam to be the case for some reason.
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 301,
data: mockResponseData
});
result = await getStatusCode();
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
describe("when expected and returned status code doesn't match", () => {
let result;
beforeAll(async () => {
// mockRejected value returns rejected promise
// which will be handled by the try/catch
axios.get.mockRejectedValue({
status: 200,
data: mockResponseData
})
result = await getStatusCode();
})
it('should return -1', () => {
expect(result).toEqual(-1)
})
});
});
getStatusCode.js
const axios = require('axios');
const qs = require('qs');
module.exports = async (options) => {
options = options || {};
options.url = options.url || {};
options.statusCode = options.statusCode || 0;
options.timeout = options.timeout || 1000;
options.maxRedirects = options.maxRedirects || 0;
try {
const response = await axios.get(options.url, {
timeout: options.timeout,
maxRedirects: options.maxRedirects,
// make all http status codes a success
validateStatus: function (status) {
return true;
}
});
return (response.status === options.statusCode) ? 1 : 0;
} catch (error) {
return -1;
}
};
【问题讨论】:
-
@Andreas 现在添加。
-
在您所指的测试中,status 为 301,statusCode 默认为 0, 301 != 0,因此返回 0。请澄清一下,您的期望究竟是什么以及原因。
-
当我提供了
status: 301的输入参数时,为什么它默认为零? -
因为
options.statusCode = options.statusCode || 0。您在一项测试中提供了{ statusCode: 200 },但在另一项测试中未提供,因此存在差异。 -
据我所知,status 和 statusCode 的唯一联系方式是它们在
return (response.status === options.statusCode) ? 1 : 0中相互比较。所以getStatusCode()没有 statusCode 选项将始终返回 0,除非响应状态为 0(状态 0 是为客户端请求错误保留的,但我不记得 Axios 是否遵循约定)。
标签: javascript node.js ecmascript-6 jestjs