RSpec: stubbing Rails.application.config value doesn't work when reopening classes?

老子叫甜甜 提交于 2019-12-03 11:57:53
leomilrib

Wow, this have been here for a long time, but in my case what I did was:

allow(Rails.configuration.your_config)
  .to receive(:[])
  .with(:your_key)
  .and_return('your desired return')

Specs passing and config values stubed correctly. =)

Now, the other thing is about your implementation, I think it would be better if you defined both methods and inside from a run or something you decided wich one to execute. Something like this:

class YourClass
  extend self

  def run
    Rails.application.config[:your_option] == 'value' ? first_method : second_method
  end

  def first_method
    # some code
  end

  def second_method
    # another code
  end
end

Hope this helps.

Edit: Oh yeah, my bad, I based my answer on this one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!