作为“模拟主义者", TDD从业者,是否应该在与被测方法相同的类中模拟其他方法? [英] As a "mockist" TDD practitioner, should I mock other methods in the same class as the method under test?

查看:60
本文介绍了作为“模拟主义者", TDD从业者,是否应该在与被测方法相同的类中模拟其他方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了马丁·福勒(Martin Fowler)的不是存根"之后,我发现我已经一直以模拟主义者"的方式练习TDD.

After reading Martin Fowler's Mocks Aren't Stubs, I've discovered I've been practicing TDD in the "mockist" fashion.

但是我想知道,即使在模拟主义者TDD中,是否也可以使嘲笑过分.

But I'm wondering if even in mockist TDD if one can take mocking too far.

这是用Python风格的伪代码进行的更新示例:

Here's an updated example in Python-style pseudo-code:

def sync_path(self):
    if self.confirm_or_create_connection():
        self.sync(self.dirpath)

confirm_or_create_connection()方法创建与服务器的连接.

The confirm_or_create_connection() method creates a connection to a server.

我在两个测试中测试了一个与此方法类似的方法,它们都模拟了confirm_or_create_connection()和sync()(即使它们都是同一类中的方法).在一个测试中,模拟的confirm_or_create_connection()返回True,然后测试确认已调用sync();在另一个测试中,模拟的confirm_or_create_connection()返回False;测试中确认未调用sync().

I tested a method similar to this in two tests, both of which mock confirm_or_create_connection() and sync() (even though they're both methods in the same class). In one test the mock confirm_or_create_connection() returns True and the test confirms that sync() was called, and in the other the mock confirm_or_create_connection() returns False and the test confirms that sync() was not called.

这合理吗?还是应该嘲笑那些用Confirm_or_create_connection()和sync()调用的对象? (我已经对这两种方法进行了其他测试.)

Is this reasonable? Or should I mock the objects that confirm_or_create_connection() and sync() call? (I have other tests of both of these methods that already do this.)

请不要通过解释我应该练习古典" TDD来回答问题.那是对另一个问题的答案:我应该练习模拟主义者还是经典的TDD?

Please don't answer the question by explaining that I should be practicing "classical" TDD instead. That's an answer to another question: Should I practice mockist or classical TDD?

推荐答案

由于某种原因,该技术被称为模拟对象",而不是模拟方法".它鼓励设计将系统划分为易于组合,协作的对象,并且远离程序代码.目的是提高抽象水平,以便您大多数情况下都是通过编写对象来进行编程的,而很少编写低级的控制流语句.

The technique is called "mock objects", not "mock methods" for a reason. It encourages designs that divide the system into easily composed, collaborating objects and away from procedural code. The aim is to raise the level of abstraction so that you mostly program by composing objects and rarely write low-level control flow statements.

这篇关于作为“模拟主义者", TDD从业者,是否应该在与被测方法相同的类中模拟其他方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆