リクエストスペック
リクエストスペックは、Railsの統合テストを薄くラップし、 フルスタックを通じて動作を駆動するように設計されています。これにはルーティング (Railsによって提供される)とスタブ化(あなたの役割)が含まれます。
リクエストスペックは type: :request でマークされ、または spec/requests に配置することで
config.infer_spec_type_from_file_location! が設定されている場合にマークされます。
リクエストスペックを使用すると、次のことができます:
- 単一のリクエストを指定する
- 複数のコントローラーをまたぐ複数のリクエストを指定する
- 複数のセッションをまたぐ複数のリクエストを指定する
統合テストに関する詳細はRailsのドキュメンテーションをご覧ください。
RSpecは、Railsのアサーションに委任する2つのマッチャを提供します:
render_template # assert_templateに委任 redirect_to # assert_redirected_toに委任
これらのメソッドの詳細については、Railsのドキュメンテーションをご覧ください。
Capybaraは リクエストスペックではサポートされていません。Capybaraの推奨される使用方法は feature specsです。
Rails統合メソッドを使用してWidgetの管理を指定する
与えられた ファイル名 "spec/requests/widget_management_spec.rb" に:
require "rails_helper"
RSpec.describe "Widget management", type: :request do
  it "creates a Widget and redirects to the Widget's page" do
    get "/widgets/new"
    expect(response).to render_template(:new)
    post "/widgets", :params => { :widget => {:name => "My Widget"} }
    expect(response).to redirect_to(assigns(:widget))
    follow_redirect!
    expect(response).to render_template(:show)
    expect(response.body).to include("Widget was successfully created.")
  end
  it "does not render a different template" do
    get "/widgets/new"
    expect(response).to_not render_template(:show)
  end
end
When I run rspec spec/requests/widget_management_spec.rb
Then the example should pass.
JSONレスポンスのリクエスト
与えられた ファイル名 "spec/requests/widget_management_spec.rb" に:
require "rails_helper"
RSpec.describe "Widget management", type: :request do
  it "creates a Widget" do
    headers = { "ACCEPT" => "application/json" }
    post "/widgets", :params => { :widget => {:name => "My Widget"} }, :headers => headers
    expect(response.content_type).to eq("application/json; charset=utf-8")
    expect(response).to have_http_status(:created)
  end
end
When I run rspec spec/requests/widget_management_spec.rb
Then the example should pass.
JSONデータの提供
与えられた ファイル名 "spec/requests/widget_management_spec.rb" に:
require "rails_helper"
RSpec.describe "Widget management", type: :request do
  it "creates a Widget and redirects to the Widget's page" do
    headers = { "CONTENT_TYPE" => "application/json" }
    post "/widgets", :params => '{ "widget": { "name":"My Widget" } }', :headers => headers
    expect(response).to redirect_to(assigns(:widget))
  end
end
When I run rspec spec/requests/widget_management_spec.rb
Then the example should pass.
エンジンルートヘルパーの使用
与えられた ファイル名 "spec/requests/widgets_spec.rb" に:
require "rails_helper"
# A very simple Rails engine
module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
  end
  class LinksController < ::ActionController::Base
    def index
      render plain: 'hit_engine_route'
    end
  end
end
MyEngine::Engine.routes.draw do
  resources :links, :only => [:index]
end
Rails.application.routes.draw do
  mount MyEngine::Engine => "/my_engine"
end
module MyEngine
  RSpec.describe "Links", type: :request do
    include Engine.routes.url_helpers
    it "redirects to a random widget" do
      get links_url
      expect(response.body).to eq('hit_engine_route')
    end
  end
end
When I run rspec spec
Then the example should pass.
サブドメイン制約付きリクエストのテスト
与えられた ファイル名 "spec/requests/widgets_spec.rb" に:
require "rails_helper"
Rails.application.routes.draw do
  resources :widgets, constraints: { subdomain: "api" }
end
RSpec.describe "Widget management", type: :request do
  before { host! "api.example.com" }
  it "creates a Widget" do
    headers = { "ACCEPT" => "application/json" }
    post "/widgets", :params => { :widget => { :name => "My Widget" } }, :headers => headers
    expect(response.content_type).to start_with("application/json")
    expect(response).to have_http_status(:created)
  end
end
私が rspec specを実行するとき
その後 例はパスするべきです。