RSpec 共享示例不得共享
这让我感到惊讶......
似乎您不能在项目中的任何地方对 2 个 rspec 共享示例组使用相同的名称。如果这样做,那么当您运行 rake spec(运行所有规范)时,rspec 会抱怨共享示例组使用非唯一名称声明。
即使对 shared_examples_for
的调用包含在描述块中(人们会认为应该确定示例的范围),也会发生这种情况。
起初,我尝试通过更改示例组的名称来解决此问题(并不理想,但我可以忍受)。
但是,当我想将示例组分解到一个单独的文件中以便我可以在多个规范文件之间共享它时,这变得更加成问题。
单独运行时,规范工作正常,但当我运行套件时,rspec 会抱怨。
`ensure_shared_example_group_name_not_taken': \
Shared example group 'a person' already exists (ArgumentError)
当然,这是一个常见问题。
我在这里缺少什么吗?
This took me by surprise…
Seems that you can’t use the same name for 2 rspec shared example groups anywhere within your project. If you do, then when you run rake spec
(to run all the specs), then rspec complains that a shared example group was declared with a non-unique name.
This happens even when the calls to shared_examples_for
are contained within a describe block (one would presume that should scope the examples).
At first I tried to work around this by changing the names of the example groups (not ideal, but I can live with it).
But this became more of a problem when I wanted to factor out the example group to a separate file so I could share it between multiple spec files.
The specs work okay when run in isolation, but when I run the suite, rspec complains.
`ensure_shared_example_group_name_not_taken': \
Shared example group 'a person' already exists (ArgumentError)
Surely this is a common problem.
Is there something I’m missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 rspec 2.6 开始,共享示例是全局的。您可以在示例组中声明它们,但它们的范围不限于该组。
As of rspec 2.6, shared examples are global. You can declare them in an example group, but they are not scoped to that group.
官方文档中有一些关于如何最好地处理此问题的提示:
https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
TL;DR
是的,共享示例是全局的。 Ruby On Rails 的最佳实践:将共享示例放在 spec/support/foo_bar_shared_examples.rb 中,然后直接开始编写
shared_example
,而不用任何describe
块或其他方式包装。There are some tips in the official documentation on how best to handle this:
https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
TL;DR
Yes, shared examples are global. Best practice for Ruby On Rails: Place shared examples in spec/support/foo_bar_shared_examples.rb and just start writing
shared_example
straight, without wrapping in anydescribe
block or otherwise.