Ruby on rails Rspec-如何存根config/environment.rb中定义的常量?

Ruby on rails Rspec-如何存根config/environment.rb中定义的常量?,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我在config/environment.rb中定义了一个名为LOCAL\u SETTINGS的常量,用于配置和在整个应用程序中使用。它是一个YAML文件,存储在config/local\u settings.yml中,有时会包含API密钥等敏感数据 我目前正在尝试为使用LOCAL\u设置[“slack”][“slack\u token”]的方法编写规范 问题是这个常数没有被我的期望值所取代。ieexpect(subject.path).to eq(help\u request)失败,因为它返回

我在
config/environment.rb
中定义了一个名为LOCAL\u SETTINGS的常量,用于配置和在整个应用程序中使用。它是一个YAML文件,存储在
config/local\u settings.yml
中,有时会包含API密钥等敏感数据

我目前正在尝试为使用
LOCAL\u设置[“slack”][“slack\u token”]
的方法编写规范

问题是这个常数没有被我的期望值所取代。ie
expect(subject.path).to eq(help\u request)
失败,因为它返回一个包含非存根
LOCAL\u设置的路径
hash

但是,如果我在
stub\u const
下面放置一个调试器,然后键入
LOCAL\u SETTINGS
,我可以看到stub\u const已经工作了

我的问题是:

  • 我可以在Rspec中做些什么来让存根为
    config/environment.rb
    中定义的常量工作吗
  • 我应该在别处简单地定义这个常数吗?如果是,在哪里?我需要在app/lib/和spec/文件夹中的整个应用程序中访问它
  • 我的
    config/environment.rb
    文件:

    # Load the Rails application.
    require_relative 'application'
    
    LOCAL_SETTINGS = YAML.load_file("#{Rails.root}/config/local_settings.yml")
    
    # Initialize the Rails application.
    Rails.application.initialize!
    
    我的规格:

    describe RequestBuilder, type: :model do
      let(:help_params) { {"user_name"=>"some_user_name", "text"=>"release-bot help"} }
      let(:help_builder) { RequestBuilder.new(help_params) }
      let(:help_request) { "/api/files.upload?file=lib%2Fresponses%2Fhelp&filetype=ruby&channels=stubbed_channel&token=stubbed_token" }
      let(:slack_settings) { {"slack"=>{"slack_token"=>"stubbed_token", "slack_channel"=>"stubbed_channel"}} }
    
      context 'Given an incoming request' do
        context 'With a correctly formatted request' do
          context 'And the "help" command' do
    
            subject { help_builder.build_request_hash }
    
            it 'builds a request containing the help data' do
              stub_const("LOCAL_SETTINGS", slack_settings)
    
              expect(subject).to have_key(:request)
              expect(subject[:request].path).to eq(help_request)
            end
          end
        end
      end
    end
    

    如果您正在使用Rails,并且config目录中已经有一个
    .yaml
    文件,我建议您查看以加载yaml文件。通过这种方式,您将能够从测试环境中隔离任何凭据,而无需为每个测试存根
    LOCAL\u设置
    const或更改任何类

    # config/local_settings.yml
    development:
        slack_token: some_dev_token
        slack_channel: some_channel
    
    test:
        slack_token: fake_token
        slack_channel: fake_channel
    
    production:
        slack_token: <%= ENV['SLACK_TOKEN'] %>
        slack_channel: <%= ENV['SLACK_CHANNEL'] %>
    
    #config/local_settings.yml
    发展:
    slack_令牌:一些_dev_令牌
    松弛信道:某些信道
    测试:
    松弛令牌:伪令牌
    松弛信道:伪信道
    制作:
    备用令牌:
    松弛信道:
    
    然后加载此配置文件:

    # config/application.rb
    module MyApp
      class Application < Rails::Application
        config.local_settings = config_for(:local_settings)
      end
    end
    
    #config/application.rb
    模块MyApp
    类应用程序
    然后您可以从
    Rails.configuration.local\u settings['slack\u token']
    而不是从
    local\u settings
    常量访问值

    另一个