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

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

问题描述

我知道我如何使用这些术语,但我想知道对于伪造嘲笑存根是否有公认的定义单元测试?你如何为你的测试定义这些?描述您可能会使用每一种的情况.

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:实现了接口但包含固定数据且没有逻辑的类.根据实现简单地返回好"或坏"数据.

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:一个实现接口的类,允许动态设置值以返回/从特定方法抛出的异常,并提供检查特定方法是否已被调用/未调用的能力叫.

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:类似于模拟类,不同之处在于它不提供验证方法已被调用/未调用的能力.

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 是我们在此讨论的内容:预先编程的对象具有期望,这些期望形成了它们期望接收的调用的规范.

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 的调用,这些值(或异常)将在 SUT 中执行未经测试的代码(请参阅第 X 页的生产错误).使用测试存根的一个关键迹象是由于无法控制 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

Mock Object 实现与 SUT(被测系统)所依赖的对象相同的接口.当我们需要进行行为验证时,我们可以使用 Mock 对象作为观察点,以避免由于无法观察在 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 来模拟要测试的接口或抽象类.事实上,你怎么称呼它并不重要,它们都是不在生产中使用的类,用作测试的实用类.

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天全站免登陆