Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock Rails::configuration

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?

like image 620
Gregory Ostermayr Avatar asked Jan 22 '26 18:01

Gregory Ostermayr


1 Answers

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.

like image 182
Simone Carletti Avatar answered Jan 24 '26 17:01

Simone Carletti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!