Sunday 15 February 2015

rspec3 - RSpec: stubbing Rails.application.config value doesn't work when reopening classes? -



rspec3 - RSpec: stubbing Rails.application.config value doesn't work when reopening classes? -

i have alternative defined in application config. class want test defined in gem (not written me). want reopen class:

myclass.class_eval if rails.application.config.myoption=='value1' # code def self.method1 end else # code def self.method2 end end end

i want test code using rspec 3:

# myclass_spec.rb require "rails_helper" rspec.describe "my class" allow(rails.application.config).to receive(:myoption).and_return('value1') context 'in taxon' 'something' expect(myclass).to respond_to(:method1) end end end

how stub application config value before running code reopens class.

wow, have been here long time, in case did was:

allow(rails.configuration.your_config) .to receive(:[]) .with(:your_key) .and_return('your desired return')

specs passing , config values stubed correctly. =)

now, other thing implementation, think improve if defined both methods , within run or decided wich 1 execute. this:

class yourclass extend self def run rails.application.config[:your_option] == 'value' ? first_method : second_method end def first_method # code end def second_method # code end end

hope helps.

edit: oh yeah, bad, based reply on one.

ruby-on-rails rspec3

No comments:

Post a Comment