ruby-on-rails - Rspec 共享定义加载两次

标签 ruby-on-rails rspec rspec-rails

我无法查明此问题的原因,但我在 Rails 项目中收到多次加载共享规范的弃用警告。以下是它们的定义方式:

#spec/support/shared/authenticated_endpoints_spec.rb

RSpec.shared_examples "an authenticated endpoint" do
  it_behaves_like "an authenticated show endpoint"
  it_behaves_like "an authenticated index endpoint"
end

RSpec.shared_examples "an authenticated show endpoint" do
# omitted
end
RSpec.shared_examples "an authenticated index endpoint" do
# omitted
end

我的 spec_helper 看起来像这样:

RSpec.configure do |config|
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
end

这是我收到的弃用警告:

WARNING: Shared example group 'an authenticated endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated show endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated index endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
The new definition will overwrite the original one.

我不需要在测试套件中的任何其他地方(无论如何我都能找到)这些共享规范。我查看了 rspec-core,发现有很多我不太了解的元编程。

有人知道如何调试吗?

最佳答案

我相信原因已描述here .由于您的文件名以“_spec.rb”结尾,它们会被 RSpec 自动加载为示例。我所做的和有帮助的是重命名文件,包含共享示例,删除“_spec”部分。

关于ruby-on-rails - Rspec 共享定义加载两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256107/

相关文章:

ruby-on-rails - RSpec - @controller 为零

ruby-on-rails - RSpec kind_of?返回错误结果

python - Ansible kitchen 测试在 debian 上执行源命令失败

ruby-on-rails-3 - 如何使用 capybara 和 rspec 单击表格行

ruby-on-rails - 当为 kvs 使用 redis 时,使用 1 db 或不同的是很快的

ruby-on-rails - Rails、Capistrano、Nginx、Unicorn - 应用程序已经初始化(RuntimeError)

ruby-on-rails - validate_presence_of 无法按预期使用增量计数器

ruby-on-rails - RSpec 功能规范找不到 Rails 引擎的路由

mysql - rails 交易

mysql - 使用 ROR 应用程序设置 mysql 数据库时出现 ArgumentError