I have a method that calls two other methods in it.
def main_method(self, query):
result = self.method_one(query)
count = self.method_two(result)
return count
def method_one(self, query):
#Do some stuff based on results.
#This method hits the database.
return result
def method_two(self, result):
#Do some stuff based on result.
#This method also hits the database.
return count
I’m not very experienced at unit testing and have never worked with Mocks and Stubs.
I’m not too sure how to create a unit test for my first method. Since method_one and method_two hit the database many times and they are very expensive, I have decided to use mox to create a mock or stub in order to eliminate the need of hitting database.
I would really appreciate it if someone who has experience working with Mocks and Stubs give me some hints on using mocks and stubs for my case.
Before worrying about testing
main_method(), first test the smaller methods. Considermethod_one(). For the purpose of discussion, let’s say it exists in a class like this:In order to test that method without hitting the database, we need an object that knows how to respond to the
all()method. For example:Now we can test it:
That’s a basic illustration. The real world is often more complicated. In order to be worth the trouble of writing the test, your mock
all()would likely do something more interesting than return a constant. Along similar lines, ifmethod_one()contains a bunch of other logic, ourMockQuerymight need to be more elaborate — that is, capable of responding appropriately to more methods. Often while trying to test code you realize that your original design was overburdened: you might need to refactormethod_one()into smaller, more tightly defined — and thus more testable — parts.Taking the same logic a step up in the hierarchy, you might create a
MockFooclass that would know how to respond in simplified ways tomethod_one()andmethod_two().