rubyconfig/config

How to use/mock in rspec?

slhck opened this issue · 3 comments

slhck commented

I would like to mock some settings temporarily for some Rspec tests.

How would I go about doing this?

When I do, inside a test:

it "tests something" do
    Settings.add_source!(
        {
          foo: {
            bar: {
              baz: false
            }
          }
        }
      )
    Settings.reload!
end

This bleeds over to other tests rather than being local to a certain test.

I can do Settings.reload_from_files without any argument to clear all settings, but I'd rather not want to manually specify the default filenames for the various environments.

I'd suggest trying to use stub_const to replace the Settings constant with a test double.

See also Constant Stubbing in RSpec 2.11

I do this in rspec:

before do
   allow(Settings.item).to receive(:sub_property).and_return('7')
end