我在反应应用程序服务中有以下方法,需要单元测试。
onDeclineCallback = () => {
console.log("boom " + document.referrer);
if (document.referrer === "") {
console.log("redirect to our age policy page");
} else {
history.back();
}
};
Run Code Online (Sandbox Code Playgroud)
我的单元测试目前看起来像:
onDeclineCallback = () => {
console.log("boom " + document.referrer);
if (document.referrer === "") {
console.log("redirect to our age policy page");
} else {
history.back();
}
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试找到一种模拟方法,document.referrer以便为每种情况编写单元测试。谁能为此提供一种方法?
您可以使用Object.defineProperty方法来设置模拟值document.referrer。
例如\nindex.ts:
export class AgeVerification {\n public onDeclineCallback = () => {\n console.log(\'boom \' + document.referrer);\n\n if (document.referrer === \'\') {\n console.log(\'redirect to our age policy page\');\n } else {\n history.back();\n }\n };\n}\nRun Code Online (Sandbox Code Playgroud)\n\nindex.spec.ts:
import { AgeVerification } from \'./\';\n\ndescribe(\'age verifiction service test\', () => {\n let ageVerification;\n beforeEach(() => {\n ageVerification = new AgeVerification();\n history.back = jest.fn();\n });\n afterAll(() => {\n jest.restoreAllMocks();\n jest.resetAllMocks();\n });\n it(\'returns user to referrer if declined and referrer is available\', () => {\n const originalReferrer = document.referrer;\n Object.defineProperty(document, \'referrer\', { value: \'Refferer Test\', configurable: true });\n ageVerification.onDeclineCallback();\n expect(history.back).toHaveBeenCalledTimes(1);\n Object.defineProperty(document, \'referrer\', { value: originalReferrer });\n });\n\n it(\'should print log\', () => {\n const logSpy = jest.spyOn(console, \'log\');\n ageVerification.onDeclineCallback();\n expect(logSpy.mock.calls[0]).toEqual([\'boom \']);\n expect(logSpy.mock.calls[1]).toEqual([\'redirect to our age policy page\']);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n100%覆盖率的单元测试结果:
\n\n PASS src/stackoverflow/59198002/index.test.ts (13.915s)\n age verifiction service test\n \xe2\x9c\x93 returns user to referrer if declined and referrer is available (17ms)\n \xe2\x9c\x93 should print log (3ms)\n\n console.log src/stackoverflow/59198002/index.ts:264\n boom Refferer Test\n\n console.log node_modules/jest-mock/build/index.js:860\n boom \n\n console.log node_modules/jest-mock/build/index.js:860\n redirect to our age policy page\n\n----------|----------|----------|----------|----------|-------------------|\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |\n----------|----------|----------|----------|----------|-------------------|\nAll files | 100 | 100 | 100 | 100 | |\n index.ts | 100 | 100 | 100 | 100 | |\n----------|----------|----------|----------|----------|-------------------|\nTest Suites: 1 passed, 1 total\nTests: 2 passed, 2 total\nSnapshots: 0 total\nTime: 15.385s\nRun Code Online (Sandbox Code Playgroud)\n\n源代码:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59198002
\n| 归档时间: |
|
| 查看次数: |
4391 次 |
| 最近记录: |