Module: RSpec::Goodies::Helpers::Stubs
- Defined in:
- lib/rspec/goodies/helpers/stubs.rb
Instance Method Summary collapse
- #stub_class_const(klass, const_string, value) ⇒ Object
-
#stub_env(name, value) ⇒ Object
Stub environment variable so that it doesn’t leak out of tests.
-
#stub_merged_hash_class_const(klass, const_string, hash) ⇒ Object
Stubs existing constant with resulting hash deep merged with existing hash and hash passed in.
-
#stub_rails_credentials(stubbed_config) ⇒ Object
Only works with nested credentials for now.
- #stub_service_as_spy(klass) ⇒ Object
-
#with_rails_logger_stubbed_as_spy ⇒ Object
Ensure scoped otherwise we get “has leaked into another example” errors.
Instance Method Details
#stub_class_const(klass, const_string, value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 12 def stub_class_const(klass, const_string, value) raise ArgumentError, "a Class or Module must be passed in" if !klass.is_a?(Class) && !klass.is_a?(Module) # Check that constant actually exists. Also by calling klass here we ensure it's loaded before stubbing # constant unless klass.const_defined?(const_string) raise Exception, "Tried to stub #{klass}::#{const_string} but it doesn't exist!" end stub_const("#{klass.name}::#{const_string}", value) end |
#stub_env(name, value) ⇒ Object
Stub environment variable so that it doesn’t leak out of tests
34 35 36 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 34 def stub_env(name, value) allow(ENV).to(receive(:[]).with(name).and_return(value)) end |
#stub_merged_hash_class_const(klass, const_string, hash) ⇒ Object
Stubs existing constant with resulting hash deep merged with existing hash and hash passed in
25 26 27 28 29 30 31 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 25 def stub_merged_hash_class_const(klass, const_string, hash) raise ArgumentError, "must pass in hash" unless hash.is_a?(Hash) existing_hash = klass.const_get(const_string) stub_class_const(klass, const_string, existing_hash.deep_merge(hash)) end |
#stub_rails_credentials(stubbed_config) ⇒ Object
Only works with nested credentials for now
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 39 def stub_rails_credentials(stubbed_config) raise NotImplementedError, "Rails not found" unless Object.const_defined?(:Rails) credentials = ::Rails.application.credentials credentials_config = credentials.config stubbed_credentials_config = credentials_config stubbed_config.each do |key, key_stubbed_config| key = key.to_sym case key_stubbed_config when Hash key_stubbed_config.each do |child_key, _| if !credentials_config.key?(key) || !credentials_config[key].key?(child_key) raise ArgumentError, "Tried to stub Rails credential #{key}: #{child_key} but it doesn't exist!" end end # Merge in existing credentials so we don't break accessing other credentials stubbed_credentials_config[key].merge!(key_stubbed_config) when String # Not nested so set it directly stubbed_credentials_config[key] = key_stubbed_config end end allow(::Rails.application).to(receive(:credentials).and_return(OpenStruct.new(stubbed_credentials_config))) end |
#stub_service_as_spy(klass) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 5 def stub_service_as_spy(klass) service_stub = class_spy(klass) stub_const(klass.to_s, service_stub) service_stub end |
#with_rails_logger_stubbed_as_spy ⇒ Object
Ensure scoped otherwise we get “has leaked into another example” errors
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rspec/goodies/helpers/stubs.rb', line 69 def with_rails_logger_stubbed_as_spy raise ArgumentError, "block required" unless block_given? original_logger = ::Rails.logger logger_spy = instance_spy("ActiveSupport::Logger") begin allow(::Rails).to(receive(:logger).and_return(logger_spy)) yield(logger_spy) ensure allow(::Rails).to(receive(:logger).and_return(original_logger)) end logger_spy end |