How to mock Rails::configuration
I'm attempting to test a class whic开发者_开发问答h 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?
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.
Rails.stubs(:configuration).returns(Rails::Application::Configuration.allocate)
This answer on mocking a Net response helped
精彩评论