在 RSpec 中,有什么区别it_behaves_like
and include_examples
?
The 文档 https://www.relishapp.com/rspec/rspec-core/v/2-14/docs/example-groups/shared-examples says:
include_examples
— 包括当前上下文中的示例
it_behaves_like "name"
— 包含嵌套上下文中的示例
但这实际上意味着什么?用另一种替换似乎对我的测试通过还是失败没有影响。在某些情况下是否有理由选择其中一种而不是另一种?
另外,还有it_should_behave_like
and it_behaves_like
只是同义词吗?
您可能知道如何使用describe
, context
, it
and specify
清晰地传达代码的一方面。提供的嵌套上下文it_behaves_like
可用于改善与读者的沟通。
我的示例将基于 RSpec 文档中给出的示例分享的例子 https://www.relishapp.com/rspec/rspec-core/v/2-14/docs/example-groups/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
如果您运行 RSpec--format documentation
你会得到以下输出:
Array
behaves like a collection
initialized with 3 items
says it has three items
initialized with 3 items
says it has three items
因此,区别在于如何读取规范,例如在发生故障时。
您喜欢哪种风格取决于您喜欢如何阅读规格的美学问题。此外,如果您在团队中工作,您建议始终使用相同的风格以提高一致性。
另外, it_should_behave_like 和 it_behaves_like 只是同义词吗?
几乎,上下文的命名不同。it should behave like ...
vs behaves like ...
。又是一个审美问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)