模拟还是存根?
我有一个方法调用其中的其他两个方法。
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
我在单元测试方面经验不是很丰富,也从未使用过 Mocks 和 Stubs。
我不太确定如何为我的第一个方法创建单元测试。由于method_one和method_two多次访问数据库并且它们非常昂贵,因此我决定使用mox来创建模拟或存根,以消除访问数据库的需要。
如果有使用模拟和存根经验的人给我一些关于在我的案例中使用模拟和存根的提示,我将非常感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在担心测试
main_method()
之前,首先测试较小的方法。考虑method_one()
。为了讨论的目的,假设它存在于这样的类中:为了在不访问数据库的情况下测试该方法,我们需要一个知道如何响应
all()
方法的对象。例如:现在我们可以测试它:
这是一个基本的说明。现实世界往往更加复杂。为了值得编写测试的麻烦,您的模拟
all()
可能会做一些比返回常量更有趣的事情。类似地,如果method_one()
包含一堆其他逻辑,我们的MockQuery
可能需要更加精细——也就是说,能够适当地响应更多方法。通常,在尝试测试代码时,您会意识到原来的设计负担过重:您可能需要将method_one()
重构为更小、定义更严格(因此更易于测试)的部分。在层次结构中采用相同的逻辑,您可以创建一个
MockFoo
类,该类知道如何以简化的方式响应method_one()
和method_two( )
。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, ourMockQuery
might 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
MockFoo
class that would know how to respond in simplified ways tomethod_one()
andmethod_two()
.