英文:
How to change return value of mocked class method
问题
我有一个模拟的节点模块类及其方法如下,以及用于测试的测试用例,我需要更改methodOne的返回值。
jest.mock("module_name", () => {
return {
__esModule: true,
Abc: class Abc {
constructor(config) {}
async methodOne(params) {
return {
message: {
content:
'我需要更改的文本',
},
};
}
},
Configuration: class Configuration {
constructor(config) {
return true;
}
},
};
});
describe("getVeganStatus", () => {
it("应处理第一种情况......", async () => {
// methodOne message.content 返回值应为“ABC”
})
it("应处理第二种情况......", async () => {
// methodOne message.content 返回值应为“XYZ”
});
})
英文:
I have a mocked class of a node module and its method as below, and test case to test case I need to change the methodOne returning value.
jest.mock("module_name", () => {
return {
__esModule: true,
Abc: class Abc {
constructor(config) {}
async methodOne(params) {
return {
message: {
content:
'This text I need to change',
},
};
}
},
Configuration: class Configuration {
constructor(config) {
return true;
}
},
};
});
describe("getVeganStatus", () => {
it("should handle case one......", async () => {
// methodOne message.content return value should be "ABC"
})
it("should handle case two......", async () => {
// methodOne message.content return value should be "XYZ"
});
})```
</details>
# 答案1
**得分**: 1
以下是你要翻译的内容:
[Replacing the mock using `mockImplementation()` or `mockImplementationOnce()`](https://jestjs.io/docs/es6-class-mocks#replacing-the-mock-using-mockimplementation-or-mockimplementationonce) 文档中的部分内容。
> 对 `jest.mock` 的调用被提升到代码的顶部。你可以稍后指定一个模拟,例如在 `beforeAll()` 中,通过在现有模拟上调用 `mockImplementation()`(或 `mockImplementationOnce()`)而不是使用工厂参数。**这还允许你在需要时在测试之间更改模拟**:
例如:
`some-module.js`:
```js
export class Abc {
constructor(config) {}
async methodOne(params) {
return {
message: {
content:
'这段文本我需要更改',
},
};
}
}
main.js:
import { Abc } from './some-module';
export async function main() {
const abc = new Abc();
return abc.methodOne().then((res) => res.message.content);
}
main.test.js:
import { main } from './main';
import { Abc } from './some-module';
jest.mock('./some-module');
describe('76863882', () => {
test('should pass 1', async () => {
Abc.mockImplementation(() => {
return {
methodOne: async () => ({ message: { content: 'ABC' } }),
};
});
const actual = await main();
expect(actual).toBe('ABC');
});
test('should pass 2', async () => {
Abc.mockImplementation(() => {
return {
methodOne: async () => ({ message: { content: 'XYZ' } }),
};
});
const actual = await main();
expect(actual).toBe('XYZ');
});
});
测试结果:
PASS stackoverflow/76863882/main.test.js (5.787 s)
76863882
✓ should pass 1 (2 ms)
✓ should pass 2
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 5.978 s, estimated 6 s
Ran all test suites related to changed files.
英文:
Following Replacing the mock using mockImplementation() or mockImplementationOnce() documentation.
> Calls to jest.mock are hoisted to the top of the code. You can specify a mock later, e.g. in beforeAll(), by calling mockImplementation() (or mockImplementationOnce()) on the existing mock instead of using the factory parameter. This also allows you to change the mock between tests, if needed:
E.g.
some-module.js:
export class Abc {
constructor(config) {}
async methodOne(params) {
return {
message: {
content:
'This text I need to change',
},
};
}
}
main.js:
import { Abc } from './some-module';
export async function main() {
const abc = new Abc();
return abc.methodOne().then((res) => res.message.content);
}
main.test.js:
import { main } from './main';
import { Abc } from './some-module';
jest.mock('./some-module');
describe('76863882', () => {
test('should pass 1', async () => {
Abc.mockImplementation(() => {
return {
methodOne: async () => ({ message: { content: 'ABC' } }),
};
});
const actual = await main();
expect(actual).toBe('ABC');
});
test('should pass 2', async () => {
Abc.mockImplementation(() => {
return {
methodOne: async () => ({ message: { content: 'XYZ' } }),
};
});
const actual = await main();
expect(actual).toBe('XYZ');
});
});
Test result:
PASS stackoverflow/76863882/main.test.js (5.787 s)
76863882
✓ should pass 1 (2 ms)
✓ should pass 2
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 5.978 s, estimated 6 s
Ran all test suites related to changed files.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论