LoginSignup
5
5

More than 5 years have passed since last update.

config.ruで色々やってるrackアプリケーションをrspecでうまくテストする

Last updated at Posted at 2015-10-29

概要

config.ru内で色々設定している場合、単純にrackアプリケーションのインスタンスを作成してもうまく動作しないときはRack::Builder.parse_fileを使おう

config.ru

config.ru
use Rack::Reloader
use AppLog, logger
use Rack::CommonLogger, logger
use Rack::Config do |env|
  env['fugafuga.app_dir'] = File.dirname(__FILE__) #この辺りが設定されない
end

map '/hogehoge' do #ここも設定されない
  run hogehoge::fuga.new
end

map '/foobar' do #ここも設定されない
  run foobar::Server.new
end

通常のパターン

specfile.rb
describe 'テスト'
  def app
    @app ||= MainApp #rackアプリケーションのインスタンス
  end
.
.
.
end

Rack::Builder.parse_fileを使う

specfile.rb
describe 'テスト'
  APPS = Rack::Builder.parse_file(File.expand_path("#{File.dirname(__FILE__)}/../config.ru")) #config.ruへのパス

  def app
    APPS.first #APPS[0](=APPS.first)→rackアプリケーションのインスタンス
  end
.
.
.
end
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5