How to unit-test 2 methods in the same class where the first method calls the second method?
Let's say I have a class MyClass
with 2 public met开发者_C百科hods, methodA()
and methodB()
, both returning the same type of object.
methodA()
will do some calculations and then call methodB
.
How should I test this class? Will I have to repeat all the tests I did for methodA()
on methodB()
?
How to approach this?
Thanks
Test both independently, because that's how clients will see them.
You should test methodA()
as if you don't know how it's implemented. Then, if you change it to use methodC()
instead of methodB()
in the future, you still have coverage.
It depends on the code being tested and how thorough you want to be. If methodA()'s outcome is effected by methodB() you could test just methodA(), however as duffymo says it's best to test both, and as robert says its best to code the test for methodA() as if you didn't know it was implemented through methodB() and the same for methodB(). Since both methodA() and methodB() are public, each should be tested separately. But it's best if the test for methodA() assures methodA() did its job and the test for methodB() assures methodB() did its job, so the tests shouldn't really be the same.
精彩评论