3

I'm attempting to test a class which makes use of the rails configuration file. I'd like to mock Rails::configuration.

I've tried things like

Rails::singleton_class.expects(:configuration).returns('result')
Rails::singleton_class.stubs(:configuration).returns('result')

How do I go about doing this?

Vega
  • 27,856
  • 27
  • 95
  • 103
Gregory Ostermayr
  • 1,123
  • 10
  • 17

2 Answers2

2
Rails.expects(:configuration).returns('result')

Please note there was a typo in your example. The returned value must be passed using returns, not return.

Also note, Rails.configuration returns Rails.application.config. If your method doesn't use Rails.configuration directly, it might actually bypass the call and your expectation won't work.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
0
Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)

This answer on mocking a Net response helped

Community
  • 1
  • 1
Gregory Ostermayr
  • 1,123
  • 10
  • 17