如何在 RSpec 中存入 Rails 秘密?

How to stub Rails secret in RSpec?

既然 Rails 带有 credential/secret 文件,我似乎无法 stub/override 使用 RSpec 的秘密。

# credentials.yml.enc
my_token: 111

我们以前用环境变量做的

allow(ENV).to receive(:[]).with('my_token').and_return('')

所以我希望能够应用与

相同的逻辑
allow(Rails.application.credentials).to receive(:my_token).and_return('')

但它不会覆盖 Rails 秘密。任何的想法?谢谢

应该像这里描述的那样直截了当https://github.com/rspec/rspec-rails/issues/2099#issuecomment-472965256

describe 'Credentials' do
  it 'stubs credentials' do
    allow(Rails.application.credentials).to receive(:my_token).and_return('123')
    expect(Rails.application.credentials.my_token).to eq('123')
  end
end