LoginSignup
1
3

More than 3 years have passed since last update.

middleware の spec を書く

Last updated at Posted at 2016-10-15

参考: rack middlewareのテストをrspecで書いてみた - Qiita

上記ページを参考に書いてみた。
テスト対象がMyMiddlewareとした場合、以下のように書ける。

require "rails_helper"

RSpec.describe MyMiddleware do
  include Rack::Test::Methods

  # テスト用アプリケーション
  class TestApplication
    def call(_)
      [200, { "Content-Type" => "text/html;charset=utf-8" }, ["test body"]]
    end
  end

  let(:test_app) { TestApplication.new }
  let(:app) { MyMiddleware.new(test_app) }

  describe "GET /" do
    it "200 を返すこと" do
      get "/"
      expect(last_response.status).to eq 200
    end
  end
end

Tips

リクエストのドメイン名を指定

get のパラメタにドメイン名を含むURL文字列を渡すと request.host にドメインが設定される。

get "http://hoge.example.com/"
# => request.host == "hoge.example.com"

参考

1
3
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
1
3