“include_examples"和“include_examples"有什么区别?和“it_behaves_like"? [英] What's the difference between "include_examples" and "it_behaves_like"?

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

问题描述

在 RSpec 中,it_behaves_likeinclude_examples 有什么区别?

In RSpec, what's the difference between it_behaves_like and include_examples?

文档 说:

include_examples — 包括当前上下文中的示例

include_examples — include(s) the examples in the current context

it_behaves_like "name" — 在嵌套上下文中包含示例

it_behaves_like "name" — include(s) the examples in a nested context

但这实际上意味着什么?用另一个替换一个似乎对我的测试通过还是失败没有影响.在某些情况下,是否有理由偏爱其中一种?

But what does this actually mean? Replacing one with the other doesn't seem to have an effect on whether my tests pass or fail. Is there a reason to prefer one over the other in some situations?

另外,it_should_behave_likeit_behaves_like 只是同义词吗?

Also, are it_should_behave_like and it_behaves_like just synonyms?

推荐答案

你可能知道如何使用 describecontextitspecify 以清楚地传达代码的一个方面.it_behaves_like 提供的嵌套上下文可用于改善与读者的这种交流.

You probably know how to use describe, context, it and specify to clearly communicate one aspect of your code. The nested context provided by it_behaves_like can be used to improve this communication with the reader.

我将基于 RSpec 文档中为 共享示例:

I will base my example on the example given in the RSpec documentation for shared examples:

shared_examples "a collection" do
  context "initialized with 3 items" do
    it "says it has three items" do
      # ...
    end
  end
end

describe Array do
  it_behaves_like "a collection"
  include_examples "a collection"
end

如果您使用 --format documentation 运行 RSpec,您会得到以下输出:

If you run RSpec with --format documentation you get the following output:

Array
  behaves like a collection
    initialized with 3 items
      says it has three items
  initialized with 3 items
    says it has three items

所以区别在于规范的读取方式,例如在失败的情况下.

So the difference is how the spec is read eg in case of a failure.

您更喜欢哪种风格是您喜欢规范阅读方式的美学问题.此外,如果您在团队中工作以提高一致性,您会建议始终使用相同的样式.

Which style you prefer is a question of aesthetics of how you like your specs to read. Furthermore you would suggest to always use the same style if you work in a team to improve consistency.

此外,it_should_behave_like 和 it_behaves_like 只是同义词吗?

Also, are it_should_behave_like and it_behaves_like just synonyms?

几乎,上下文的命名不同.它应该表现得像... vs 表现得像....又是一个美学问题.

Almost, the context is named differently. it should behave like ... vs behaves like .... Again a question of aesthetics.

这篇关于“include_examples"和“include_examples"有什么区别?和“it_behaves_like"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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