rubyconfig / config

Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.
Other
2.1k stars 230 forks source link

How to use/mock in rspec? #336

Closed slhck closed 1 year ago

slhck commented 1 year ago

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.

cjlarose commented 1 year ago

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

jcoyne commented 1 year ago

I do this in rspec:

before do
   allow(Settings.item).to receive(:sub_property).and_return('7')
end
deepakmahakale commented 6 months ago

Minitest

# Settings.some_boolean
Settings.stub :some_boolean, true do
  # method_call
end

# Settings.app.some_boolean
Settings.app.stub :some_boolean, true do
  # method_call
end