Search code examples
ruby-on-railsrspecspecs

how to use if statement to do a conditional check inside specs?


I have this config/initializer.rb which lets me load a yaml /config/application.yaml and do something like APP_CONFIG["myvar"] how could I enable this for my SPECS also?

My goal is to do something like:

require "spec_helper"

describe BetaController do
  describe "routing" do

    if APP_CONFIG["viral"] and APP_CONFIG["beta"]

      it "routes to #index do" do
        get("/").should route_to("home#index")
      end

    end

  end
end

Solution

  • I would suggest using this gem:

    https://github.com/oshuma/app_config

    Given this YAML file:
    
    ---
    admin_email: 'admin@example.com'
    api_name:    'Supr Webz 2.0'
    api_key:     'SUPERAWESOMESERVICE'
    Use it like so:
    
    AppConfig.setup!(yaml: '/path/to/app_config.yml')
    
    # Later on...
    AppConfig.admin_email  # => 'admin@example.com'
    AppConfig.api_name     # => 'Supr Webz 2.0'
    AppConfig.api_key      # => 'SUPERAWESOMESERVICE'
    

    This could easily be added to your spec_helper