I have written a custom Ruby Gem to hook into our company’s authentication and authorization system and am starting to develop the unit tests for the gem.
In our rails app, the Gem can be configured via environment.rb and a custom initializer and yaml file containing the configuration values.
I need to translate the configuration of the Gem in rails to test the standalone Gem. How do I translate this over to Rspec to perform integration testing??
Gem configuration in rails
# environment.rb
MyGem.configure do |config|
config.url = MY_CONFIG ['url']
config.application_name = MY_CONFIG ['app_name']
config.application_id = MY_CONFIG ['app_id']
config.logger = Rails.logger
config.log_level = :debug
# Rails config/initalizers/load_config.rb
# Custom config file loading automatically done via initializers
MY_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/my_config.yml")[Rails.env]
# config/my_config.yml
defaults: &defaults
url: http://url/to/service
app_name: my app
app_id: 1
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
end
Here’s a simple project where you can see how you’d go by doing it: multiplier
First and foremost, if you’re doing the gem management by yourself, please don’t, use helper tools like jeweler to do it for you. Install the jeweler gem (gem install jeweler) and once you have it installed, create your gem projet:
With this, it’s going to setup a skeleton gem that’s going to have a single main file (where you would require your necessary gem files) and the spec folder.
At the spec folder there’s spec_helper.rb, that’s where our configuration lives, what I did was:
So, here lives the config for our gem, but you could even do it on each spec, if you’d need it. But if you want to use a single config for all specs this is where you should place it.