Ruby on rails RSpec中不允许使用Helper方法

Ruby on rails RSpec中不允许使用Helper方法,ruby-on-rails,rspec,Ruby On Rails,Rspec,这是我试图做的一个简化版本。我有一个名为helper\u method的助手方法,它只是运行一个SQL查询来获取一些数据。此数据用于确定我的响应错误是否正确返回sanitize\u and\u execute只是对ActiveRecord::Base.connection.execute(sql)调用的包装 require'spec\u helper' 包括Db#一个sanitize_和_execute来自的模块 定义MyModel do 让。。。 让。。。 def帮助器_方法(供应商_id)

这是我试图做的一个简化版本。我有一个名为
helper\u method
的助手方法,它只是运行一个SQL查询来获取一些数据。此数据用于确定我的响应错误是否正确返回
sanitize\u and\u execute
只是对
ActiveRecord::Base.connection.execute(sql)
调用的包装

require'spec\u helper'
包括Db#一个sanitize_和_execute来自的模块
定义MyModel do
让。。。
让。。。
def帮助器_方法(供应商_id)

query=RSpec中有很多东西是上下文的,在该上下文中定义一个方法,但在错误的上下文中调用它不会改变这种限制。@tadman是的,我看到了,但我不确定如何解决这种情况下的上下文问题。在这种情况下,
Db
是什么?
require 'spec_helper'
include Db # a module where sanitize_and_execute comes from

define MyModel do
  let...
  let...
  def helper_method(vendor_id)
    query = <<~SQL.squish
      select distinct thing1, thing2
       from thing
       where id = (?)
    SQL
    sanitize_and_execute([query, id])
  end

  context 'some context' do
    let(:my_model) { create(:my_model) }

    it 'returns the correct error' do
      expect(response.error).to include "error: #{helper_method(my_model.id)}"
    end
  end

end
require 'spec_helper'
define MyModel do
  # Include into context of the example
  include Db # a module where sanitize_and_execute comes from
  # ...

  # But why not just use a model instead? ¯\_(ツ)_/¯
  def helper_method(vendor_id)
    query = Arel::Table.new('thing').then do |tbl|
      tbl.project(tbl[:thing1], , tbl[:thing1])
         .distinct
         .where(tbl[:id]).eq(vendor_id)
    end
    sanitize_and_execute(query.to_sql)
  end
end