π€ 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
- ruby on rails - Rspec test to render no layout - Stack Overflow
- Method: Spec::Rails::Matchers#render_template β Documentation for rspec-rails (1.3.4) β
Top comments (0)