【发布时间】:2019-12-21 13:41:35
【问题描述】:
最终目标是包含在 rails_helper.rb 中的 shared_examples.rb。 shared_examples.rb 是该文件的副本
我想在我的黄瓜测试中引用 shared_examples.rb 以便我可以使用方法it_behaves_like 'two_factor_authenticatable'
我有以下文件夹结构:
更新以包含@morissetcl 建议的结构
features
step_definitions
sample_step.rb
support
env.rb
sample.feature
spec
models
user_spec.rb
support
shared_examples
shared_example.rb
rails_helper.rb
spec_helper.rb
features 和 spec 文件夹都位于我的 rails 项目的根目录中。
我试图将位于 spec 文件夹中的 rails_helper.rb 包含在 sample_step.rb 文件中。
我尝试在 sample_step.rb 文件中使用不同类型的 require,如下所示。
需要'spec/spec_helpers/shared_examples'
需要'../../spec/spec_helpers/shared_examples'
require_relative '../../spec/spec_helpers/shared_examples'
我不断收到以下错误
undefined method `it_behaves_like' for main:Object (NoMethodError)
【问题讨论】:
-
好的做法是将您的
shared_example放在支持文件夹spec/support/shared_examples/the_name_of_your_shared.rb中,然后在您的rails_helper添加以下行Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}将用于加载您的文件(在此方式你不需要什么东西)。此外,我想将spec_helper用作文件夹会令人困惑。 -
感谢您的建议。我确实进行了您提供的更改,但是,我的共享示例仍然找不到
it_behaves_like方法。这是错误的 sn-p:undefined method it_behaves_like for #<Cucumber::Rails::World:0x00007fe383683968> (NoMethodError) -
也许真正要问的问题是黄瓜是否允许包含来自 rspec 的共享示例? shared_example 是一个 rspec 共享示例。
标签: ruby cucumber ruby-on-rails-5 rspec-rails