I'm trying to test a lib I've created. It auto load with an initializer.
Here is what I have
# config/initializer/my_lib.rb
MyLib.configure do |config|
config.salt = Rails.application.secrets.encrypted_salt
config.secret = Rails.application.secrets.encrypted_secret
end
# lib/my_lib.rb
class MyLib
class << self
attr_accessor :configuration
def config
@configuration ||= Configuration.new
end
def configure
yield(config)
raise 'Salt not specified' unless config.salt
raise 'Secret not specified' unless config.secret
end
end
class Configuration
attr_accessor :salt, :secret
end
end
salt
secret
MyLib.configure
RuntimeError
allow(described_class).to receive(:configure) do |&config|
config.salt = nil
config.secret = nil
end
|config|
|&config|
nil
config
Use expect error:
expect do
MyLib.configure do |config|
config.salt = nil
config.secret = nil
end
end.to raise_error RuntimeError
But I would recommend that you create your own exception class such as MyLib::ConfigurationError
as RuntimeError could have other causes and the test could actually pass if there is a bug in the code.