问题:
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. 描述您可能会使用每种情况的情况。
Here is how I use them: 这是我的用法:
Fake : a class that implements an interface but contains fixed data and no logic. Fake :实现接口但包含固定数据且没有逻辑的类。 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. Mock :一个实现接口并允许动态设置要返回的值/从特定方法抛出异常的类,并提供检查是否已调用/未调用特定方法的能力。
Stub : Like a mock class, except that it doesn't provide the ability to verify that methods have been called/not called. 存根(Stub) :类似于模拟类,不同之处在于它不提供验证方法是否已被调用的能力。
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. 我主要使用伪造的类来抽象出数据依赖性,或者当模拟/存根过于繁琐而无法每次设置时。
解决方案:
参考一: https://stackoom.com/question/1S6e/伪造-嘲笑和存根之间有什么区别参考二: https://oldbug.net/q/1S6e/What-s-the-difference-between-faking-mocking-and-stubbing
来源:oschina
链接:https://my.oschina.net/u/4428122/blog/4358144