[英]RSpec testing destroy controller action throws an error: ActionView::MissingTemplate
I am writing tests for my controller and I get keep getting the error ActionView::MissingTemplate
.我正在为我的控制器编写测试,并且不断收到错误ActionView::MissingTemplate
。 I have looked at related questions asked before but I can't seem to get it working with my scenario.我已经查看了之前提出的相关问题,但我似乎无法将其用于我的场景。
Here is my controller code:这是我的控制器代码:
def confirm_destroy
render :layout => 'overlay'
end
def destroy
@role.destroy
end
And here is the test I have written:这是我写的测试:
describe 'DELETE #destroy' do
let!(:role) { create(:role, user: current_user) }
let(:params) { {id: role.id, format: :json} }
it 'performs a delete' do
expect { delete :destroy, params }.to change { Role.count }.by(-1)
end
end
when I run the test I get the error:当我运行测试时,出现错误:
ActionView::MissingTemplate:
Missing template company/settings/roles/destroy, application/destroy with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :slim]}. Searched in:
* "/my_app/app/views"
* "/usr/local/bundle/gems/devise-4.5.0/app/views"
Any idea why this is so?知道为什么会这样吗? Thanks.谢谢。
When controller destroy
action is finished, Rails tries to render a default template (view) and it's not defined, meaning you don't have file my_app/app/views/company/settings/roles/destroy.json
.当控制器destroy
操作完成时,Rails 会尝试渲染一个默认模板(视图)并且它没有被定义,这意味着你没有文件my_app/app/views/company/settings/roles/destroy.json
。
You have several options to fix it:您有多种选择来修复它:
my_app/app/views/company/settings/roles/destroy.json
and it will be rendered (not advised for JSON).定义my_app/app/views/company/settings/roles/destroy.json
它将被呈现(不建议用于 JSON)。index
(again, not advised for JSON).重定向到其他操作,例如index
(同样,不建议用于 JSON)。-- ——
# render nothing:
def destroy
@role.destroy
head :ok
end
# render json:
# render nothing:
def destroy
@role.destroy
render json: { deleted: true }, status: :no_content
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.