レンダリンクされたかどうか
render_template
マッチャー
render_template matcher
render_template
マッチャーは与えられたテンプレートまたはレイアウトをレンダーする要求を明示するために使われる。assert_template
へ指名する
The render_template matcher is used to specify that a request renders a given template or layout. It delegates to assert_template
リクエスト仕様書とコントローラ仕様書で使えます
It is available in controller specs (spec/controllers) and request specs (spec/requests).
注意:ender_template
だけでなくリダイレクトに対してredirect_to(:action => 'new')
を使う,
NOTE: use redirect_to(:action => 'new') for redirects, not render_template.
三つの可能なオプション付きrender_template
の使用
Using render_template with three possible options
Given a file named “spec/controllers/gadgets_spec.rb” with:
require "rails_helper"
RSpec.describe GadgetsController do
describe "GET #index" do
subject { get :index }
it "renders the index template" do
expect(subject).to render_template(:index)
expect(subject).to render_template("index")
expect(subject).to render_template("gadgets/index")
end
it "does not render a different template" do
expect(subject).to_not render_template("gadgets/show")
end
end
end
render_views
でレンダリングされていないから内容まではわからない
require "rails_helper"
RSpec.describe UsersController,type: :controller do
# render_views
# createアクション
describe "newアクション" do
it "newページがレンダリングされる" do
get "new"
expect(response.body).to render_template(:new)
end
it "newページに新規登録が含まれている" do
get "new"
expect(response.body).to include("新規登録")
end
end
end
UsersController
newアクション
user/newページがレンダリングされる
user/newページに新規登録が含まれている (FAILED - 1)
Failures:
1) UsersController newアクション user/newページに新規登録が含まれている
Failure/Error: expect(response.body).to include("新規登録")
expected "" to include "新規登録"
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.06515 seconds (files took 1.21 seconds to load)
2 examples, 1 failure