英文:
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
根据[使用`mockImplementation()`或`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:
'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');
});
});
测试结果:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论