LoginSignup
1
0

More than 5 years have passed since last update.

render file: 'file_name', layout: falseをテストする。

Posted at

イマイチちゃんとしたやり方が見つからなかったので、取り敢えず現状のテスト状態を記述。
ちゃんとした方法が知りたい。

render file: 'file_name', layout: false

Controller部分はこんな感じで

def show
  @object = Object.find(params[:id])
  render file: 'file_name.html', layout: false
end

単純にファイルを表示して、
可能ならspec内に

# bra bra bra...
  get :show, id: test_object
  expect(response).to render_template(file: "some_file.html", layout: false)

と言う感じでテストしたいのだけど、出来ない。

  1) ObjectsController GET #show renders file
     Failure/Error: expect(response).to render_template(file: "some_file.html", layout: false)
       Expected [] to include "some_file.html".

となってしまう。
色々調べるとインスタンス変数@templatesとか@layoutsがあるらしくそれでテスト出来るっぽいのだが見つからない。なんだか@_templatesとか@_layoutsは見つかったのでそれでテストすることに。

# bra bra bra...
  get: show, id: test_object
  expect(@_layouts).to be_blank
  expect(@_templates.keys.first).to eq('some_file.html')

一応ちゃんとテスト出来てるけど、適当に見つけた方法なのでちゃんとした方法がありそうで知りたい。

参考

ruby on rails - Rspec test to render no layout - Stack Overflow

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