Ruby on rails 带rspec的短梗费加罗

Ruby on rails 带rspec的短梗费加罗,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我在rspec测试中对费加罗有问题。 我有如下代码: class User < ActiveRecord::Base def ce_url_link path = Figaro.env.online_url || Figaro.env.root_url + '/online' "#{path}/#{ce_code}" end end describe 'ce_url_link' do let(:user) { create(:user) } cont

我在rspec测试中对费加罗有问题。 我有如下代码:

class User < ActiveRecord::Base

  def ce_url_link
    path = Figaro.env.online_url || Figaro.env.root_url + '/online'
    "#{path}/#{ce_code}"
  end
end


describe 'ce_url_link' do
  let(:user) { create(:user) }

  context 'when Figaro.env.online_url is not present' do

    it 'uses Figaro.env.root_url as a path' do
      allow(Figaro.env).to receive(:online_url).and_return(nil)
    end
  end
end
class用户

当我添加一些期望时,spec Figaro.env.online\u url返回与application.yml中相同的结果。为什么会这样?

你还没有把费加罗.env作为替身

describe do
  before do
    stub_const("Figaro", double)
    allow(Figaro).to receive_message_chain(:env, :foo_bar) { :yay }
  end

  it do
    expect(Figaro.env.foo_bar).to eq :yay
  end
end