Subscribed unsubscribe Subscribe Subscribe

Rack middlewareのテスト

require 'rack/test'

describe Rack::HogeMiddleware do
  include Rack::Test::Methods

  subject(:response) { last_response }

  let(:app) do
    this_plugin = described_class

    Rack::Builder.new do
      use this_plugin
      map '/foo' do
        run lambda { |env| [200, {'Content-Type' => 'text/html'}, '<html></html>'] }
      end
      map '/bar' do
        run lambda { |env| [200, {'Content-Type' => 'text/html'}, '<body></body>'] }
      end
    end.to_app
  end
  
  before do
    get '/'
  end

  it 'なんかテスト書く' do
    # response.body.should # なんとか~
  end
end

こんな感じdummyのアプリを作ってテスト書いていけるので便利です。