伪造,嘲笑和存根之间有什么区别? [英] What's the difference between faking, mocking, and stubbing?

查看:76
本文介绍了伪造,嘲笑和存根之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我如何使用这些术语,但是我想知道是否存在针对以下产品的伪造模拟存根的定义.单元测试?您如何为测试定义这些?描述您可能会使用每种情况的情况.

I know how I use these terms, but I'm wondering if there are accepted definitions for faking, mocking, and stubbing for unit tests? How do you define these for your tests? Describe situations where you might use each.

这是我的使用方式:

伪造:一个实现接口但包含固定数据且没有逻辑的类.只需根据实现返回好"或坏"数据即可.

Fake: a class that implements an interface but contains fixed data and no logic. Simply returns "good" or "bad" data depending on the implementation.

模拟:一个实现接口并允许动态设置值以返回/从特定方法抛出异常的类,并提供检查是否已调用特定方法的能力.叫.

Mock: a class that implements an interface and allows the ability to dynamically set the values to return/exceptions to throw from particular methods and provides the ability to check if particular methods have been called/not called.

存根:类似于模拟类,除了它不提供验证是否已调用方法的能力.

Stub: Like a mock class, except that it doesn't provide the ability to verify that methods have been called/not called.

模拟和存根可以手动生成,也可以由模拟框架生成.伪类是手工生成的.我主要使用模拟来验证我的类和依赖类之间的交互.一旦验证了交互作用并测试了代码中的替代路径,便会使用存根.我主要使用伪造的类来抽象出数据依赖关系,或者当模拟/存根过于繁琐而无法每次设置时.

Mocks and stubs can be hand generated or generated by a mocking framework. Fake classes are generated by hand. I use mocks primarily to verify interactions between my class and dependent classes. I use stubs once I have verified the interactions and am testing alternate paths through my code. I use fake classes primarily to abstract out data dependencies or when mocks/stubs are too tedious to set up each time.

推荐答案

您可以获取一些信息:

伪造对象实际上具有有效的实现,但通常采取一些捷径,这使其不适用于生产

Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production

存根为测试期间进行的通话提供固定答案,通常不会对测试中所编程的内容完全响应.存根还可以记录有关呼叫的信息,例如电子邮件网关存根,它可以记住已发送"的消息,或者仅记住已发送"的消息数量.

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.

模拟是我们在这里所说的:预先编程并带有期望的对象,这些对象构成了期望接收的呼叫的规范.

Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

伪造:我们获取或构建一个非常轻量级的功能,实现与SUT所依赖的组件所提供的功能相同的功能,并指示SUT使用它而不是真实的功能.

Fake: We acquire or build a very lightweight implementation of the same functionality as provided by a component that the SUT depends on and instruct the SUT to use it instead of the real.

存根:此实现被配置为使用将在SUT中执行未经测试的代码(请参阅第X页的生产错误)的值(或异常)来响应来自SUT的调用.使用测试存根的一个关键指示是由于无法控制SUT的间接输入而导致未测试代码

Stub : This implementation is configured to respond to calls from the SUT with the values (or exceptions) that will exercise the Untested Code (see Production Bugs on page X) within the SUT. A key indication for using a Test Stub is having Untested Code caused by the inability to control the indirect inputs of the SUT

模拟对象,其实现与SUT(被测系统)所依赖的对象相同的接口.当我们需要进行行为验证时,可以使用模拟对象作为观察点,以避免因无法观察调用方法对SUT的副作用而导致未经测试的需求(请参阅第X页的生产错误).

Mock Object that implements the same interface as an object on which the SUT (System Under Test) depends. We can use a Mock Object as an observation point when we need to do Behavior Verification to avoid having an Untested Requirement (see Production Bugs on page X) caused by an inability to observe side-effects of invoking methods on the SUT.

我尝试通过使用:Mock和Stub来简化.当它是一个返回设置为测试类的值的对象时,我会使用Mock.我使用Stub模仿要测试的Interface或Abstract类.实际上,您所说的并不重要,它们都是生产中不使用的类,并且用作测试的实用程序类.

I try to simplify by using : Mock and Stub. I use Mock when it's an object that returns a value that is set to the tested class. I use Stub to mimic an Interface or Abstract class to be tested. In fact, it doesn't really matter what you call it, they are all classes that aren't used in production, and are used as utility classes for testing.

这篇关于伪造,嘲笑和存根之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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