DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

[RSpec] layout: false, how to test it?

πŸ€” Situation

# HogeController def show return render :expired, layout: false if @hoge.expired? end 

Test matrix

@hoge.expired? view_template layout
true views/hoges/expired.html.erb -
false views/hoges/show.html.erb layouts/application.html.erb

​

πŸ‘ Solution

​

# hoges_controller_spec.rb subject { get :show, id: hoge.id } context 'expired' do let(:hoge) { create(:hoge, :expired) } it do expect(subject).to be_success # πŸ¦„ write the layout: [] explicitly expect(subject).to render_template(template: :expired, layout: []) end end context 'valid' do let(:hoge) { create(:hoge, :valid) } it do expect(subject).to be_success expect(subject).to render_template(template: :show, layout: 'layouts/application') end end 

​

πŸ‘Ž Falsenegative Testing

If you don't specify the template and layout, the render_template assert only the template.
​

Example

expect(subject).to render_template(:show, 'layouts/application') 

​
Here, @expected should be "show, layouts/application".

[2] render_template :show, 'layouts/application' => #<RSpec::Rails::Matchers::RenderTemplate::RenderTemplateMatcher:0x00007ffd90dba7fc @expected="show", πŸ‘Ž @message="layouts/application", πŸ‘Ž @redirect_is=nil, @scope=# 

​
​## πŸ“š References


πŸ”— Parent Note

Top comments (0)