admin管理员组文章数量:1325236
I have created a class with some methods and I want to mock the methods inside that class so I tried to use spyOn() but it is not working any idea what could be wrong? I am using myClass.prototype instead of myClass in spyOn() but still it does not work.
class myClass {
constructor(){
}
_methodA () {
}
_methodB() {
}
main () {
const res1 = _methodA();
const res2 = _methodB();
}
}
tests:
it('Testing' , () => {
// Some mock data passed below
jest.spyOn(myClass.prototype, '_methodA').mockReturnValue(mockData1)
jest.spyOn(myClass.prototype, '_methodB').mockReturnValue(mockData2)
const obj = new myClass();
obj.main();
expect(myClass._methodA).toHaveBeenCalledTimes(1);
expect(myClass._methodB).toHaveBeenCalledTimes(1);
});
I have created a class with some methods and I want to mock the methods inside that class so I tried to use spyOn() but it is not working any idea what could be wrong? I am using myClass.prototype instead of myClass in spyOn() but still it does not work.
class myClass {
constructor(){
}
_methodA () {
}
_methodB() {
}
main () {
const res1 = _methodA();
const res2 = _methodB();
}
}
tests:
it('Testing' , () => {
// Some mock data passed below
jest.spyOn(myClass.prototype, '_methodA').mockReturnValue(mockData1)
jest.spyOn(myClass.prototype, '_methodB').mockReturnValue(mockData2)
const obj = new myClass();
obj.main();
expect(myClass._methodA).toHaveBeenCalledTimes(1);
expect(myClass._methodB).toHaveBeenCalledTimes(1);
});
Share
Improve this question
edited Dec 8, 2020 at 14:35
jonrsharpe
122k30 gold badges267 silver badges474 bronze badges
asked Dec 8, 2020 at 14:35
rock stonerock stone
4933 gold badges10 silver badges21 bronze badges
6
- Don't spy on the thing you're supposed to be testing. The system under test is the class, internal method usage is an implementation detail. – jonrsharpe Commented Dec 8, 2020 at 14:37
- @jonrsharpe Sorry did not get your point can you please ellaborate? – rock stone Commented Dec 8, 2020 at 14:39
- @jonrsharpe Do you mean to say I should not test methods inside class? – rock stone Commented Dec 8, 2020 at 14:40
- 1 I mean you shouldn't test that main uses other methods inside the same class, test the overall behaviour instead. It seems like methodA and methodB are effectively private, and private methods shouldn't be tested directly. – jonrsharpe Commented Dec 8, 2020 at 14:55
- @jonrsharpe How to test private methods or we should not test private methods? – rock stone Commented Dec 8, 2020 at 15:13
1 Answer
Reset to default 6You can spy the class instance methods:
it("Testing", () => {
// Some mock data passed below
const obj = new myClass();
const spyA = jest.spyOn(obj, "_methodA").mockReturnValue({});
const spyB = jest.spyOn(obj, "_methodB").mockReturnValue({});
obj.main();
expect(spyA).toHaveBeenCalledTimes(1);
expect(spyB).toHaveBeenCalledTimes(1);
});
You can spy the class methods:
it("Testing", () => {
// Some mock data passed below
const spyA = jest.spyOn(myClass.prototype, "_methodA").mockReturnValue({});
const spyB = jest.spyOn(myClass.prototype, "_methodB").mockReturnValue({});
const obj = new myClass();
obj.main();
expect(spyA).toHaveBeenCalledTimes(1);
expect(spyB).toHaveBeenCalledTimes(1);
});
The other case using Db data is possible too:
it("Testing", () => {
const obj = new classA();
obj.main();
const spyA = jest.spyOn(DBClass.prototype, "getData").mockReturnValue({}); // DBClass could be a db class or a mock class
expect(spyA).toHaveBeenCalledTimes(1);
});
本文标签: javascriptJest spyOn not working for ES6 class methodsStack Overflow
版权声明:本文标题:javascript - Jest spyOn not working for ES6 class methods? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166540a2425929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论