如何解决rails / rspec将render_template方法读取为assert_template'NoMethodError Exception'
我想测试我的根路径是否呈现正确的视图:
require 'rails_helper'
RSpec.describe "Statics",type: :request do
describe "GET root path" do
it "returns http success" do
get "/"
expect(response).to have_http_status(:success)
end
it 'routes GET / to static#landing_page' do
expect('/').to render_template('static#landing_page')
end
end
end
第二次测试失败。为了找出背后的原因,我用byebug手动输入了第二条命令。然后,我收到此错误消息:
*** NoMethodError异常:assert_template已被提取到gem。要继续使用它, 将
gem 'rails-controller-testing'
添加到您的Gemfile中。
出于某种原因,我不太确定rspec是否将render_template与assert_template方法混淆并失败。如何修复它才能通过此测试?
解决方法
以RSpec docs状态:
render_template匹配器用于指定请求渲染 给定的模板或布局。它委托给assert_template
在控制器规格(规格/控制器)和要求中可用 规格(规格/要求)。
注意:使用redirect_to(:action =>'new')进行重定向,而不是 render_template。
所以,这里不要混淆。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。