在 RSpec 中,mock 和 double 有什么区别?
在 rspec 中,您可以创建一个 mock
或一个 double
。这两者似乎几乎是同一件事,我在文档中找不到任何可以消除它们歧义的内容。
有什么区别?
In rspec you can either create a mock
or a double
. These two seem to be almost the same thing and I can't find anything in the documentation that disambiguates them.
What's the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
mock
和stub
都是更通用的double
的别名。与context
和describe
一样,它们可以互换使用,以使规范的意图更加清晰。 The RSpec Book对此进行了更详细的描述。Both
mock
andstub
are aliases of the more genericdouble
. Likecontext
anddescribe
, they can be used interchangeably to make the intent of the specs more clear. This is described in a lot more detail in The RSpec Book.自从
以来,这似乎只是别名 >:__declared_as
似乎没有被使用,但用于消息。The seem to be just aliases since
:__declared_as
doesn't seem to be used but for messages.。
当我们依赖具有不确定性特征的组件时,我们可能会发现文件损坏、磁盘故障、网络超时以及服务器在运行规范过程中宕机 因为这些是我们无法控制的事情,所以当我们运行规范时,它们可能会导致不一致和令人惊讶的结果。双精度数可以将我们的示例与这些依赖项的实际实现脱节。
存根
时系统行为基于一个序列。存根对此来说是完美的。因为每个示例都可以指定不同的序列。示例:- 在随机生成器的情况下,它显然是不确定性的来源。我们想用稳定序列替换真正的随机生成器。
模拟
有时我们需要来自可能尚不存在的另一个对象的某些服务。在这种情况下,我们可以引入模拟对象。我们可以对其进行编程,使其表现得像我们当前期望的对象。因此,当我们专注于交互模拟对象时,它会更容易实现。
doubles
when we depend on components with nondeterministic characteristics, we may find that files get corrupted, disk fail, networks timeout, and servers go down in the middle of running specs. because these are things that we have no control over, they can lead to inconsistent and surprising results when we run our specs. doubles can disconnect our examples from real implementations of these dependencies.
stub
when the system behaviour based on a sequence. a stub is perfect for this .Because each example can specify a different sequence.example:- In case of random generator, it is clearly a source of non determination. we want to replace the real random generator with stable sequence.
Mocks
some time we need some service from another object that may not yet exist. In cases like this we can introduce mock object. which we can program to behave as the object we are currently expects. so when we focus on interaction mock objects make it much easier to achieve.