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)