rspec如何在示例组中重用代码

rspec如何在示例组中重用代码,rspec,Rspec,我想做的是: describe Specs::Specs do shared_context 'common' do def get_property(type) subject.send(type) end end describe 'attributes not needing conversion' do include_context 'common' # USE THE CODE HERE get_property(:w

我想做的是:

describe Specs::Specs do
  shared_context 'common' do
    def get_property(type)
      subject.send(type)
    end
  end

  describe 'attributes not needing conversion' do
    include_context 'common'
    # USE THE CODE HERE
    get_property(:weight)
  end
end
我想停止设置示例,但我不断遇到以下错误:

示例组(例如
描述
上下文
块)上没有
get\u属性
。它只能从单个示例(例如
It
块)或在示例范围内运行的构造(例如
before
let
等)中获得

如何使嵌套示例组中的方法可用?
如何使嵌套示例组中的变量可用?

当我在rspec中需要帮助器方法时,我创建了一个单独的模块。我将模块保存为/spec/support/helper_method.rb,然后在spec_helper或rails_helper.rb中的“RSpec.configure do | config |”块中添加了“config.include HelperMethod”


进一步解释此配置。

当我在rspec中需要helper方法时,我创建了一个单独的模块。我将模块保存为/spec/support/helper_method.rb,然后在spec_helper或rails_helper.rb中的“RSpec.configure do | config |”块中添加了“config.include HelperMethod”


进一步解释此配置。

我认为您可以通过示例所示的
'include\u shared=>true'
,我认为您可以通过示例所示的
'include\u shared=>true'