Gregory Ostermayr
Gregory Ostermayr

Reputation: 1121

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?

Upvotes: 3

Views: 1963

Answers (2)

Gregory Ostermayr
Gregory Ostermayr

Reputation: 1121

Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)

This answer on mocking a Net response helped

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176562

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.

Upvotes: 2

Related Questions