Rails 测试 "ActionController::UnknownFormat: is missing a template for this request format and variant"
Rails Test "ActionController::UnknownFormat: is missing a template for this request format and variant"
我正在为名为 "Canary" 的 Twitter 克隆应用程序创建测试套件,但我很难为 chirps#reply 和 chirps#rechirps
编写集成测试
Chirps 控制器
def reply
@chirp = current_user.chirps.new
@parent = Chirp.find_by(id: params[:parent_id])
@chirp.parent_id = params[:parent_id]
respond_to do |format|
format.js
format.html
end
end
def rechirp
@chirp = current_user.chirps.new
@reference = Chirp.find_by(id: params[:reference_id])
@chirp.reference_id = params[:reference_id]
respond_to do |format|
format.js
format.html
end
end
啁啾视图
<%= link_to reply_chirp_path(current_user, parent_id: chirp.id), remote: true, data: { target: '#modal_container', toggle: 'modal' } do %>
<%= icon('far', 'comment') %> <%= chirp.children.size if chirp.children.size > 0 %>
<% end %>
<%= link_to rechirp_chirp_path(current_user, reference_id: chirp.id), remote: true, data: { target: '#modal_container', toggle: 'modal' } do %>
<%= icon('fas', 'retweet') %> <%= count_rechirps(chirp) %>
<% end %>
Reply.js.erb
$("#modal_container").find(".modal-content").html("<%= j render 'chirps/modal_reply' %>");
$("#modal_container").modal('show', 'focus');
Rechirp.js.erb
$("#modal_container").find(".modal-content").html("<%= j render 'chirps/modal_rechirp' %>");
$("#modal_container").modal('show', 'focus');
Chirps 接口测试
# Try to reply to chirp
assert_select 'a[href=?]', reply_chirp_path(@user, parent_id: @chirp.id)
assert_difference 'Chirp.count', 1 do
get reply_chirp_path(@user, parent_id: @chirp)
assert_select 'div#modal_container > div.modal-dialog > div.modal-content > div.modal-body'
post chirps_path, params: { chirp: { content: content, parent_id: @chirp.id } }
assert :success
end
assert_redirected_to root_url
follow_redirect!
# Try to rechirp chirp
assert_select 'a[href=?]', rechirp_chirp_path(@user, reference_id: @chirp.id)
当我 运行 chirp_interface_test.rb 时,我一直遇到同样的错误:
Error:
ChirpsInterfaceTest#test_chirp_interface:
ActionController::UnknownFormat: ChirpsController#reply is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
test/integration/chirps_interface_test.rb:30:in `block (2 levels) in <class:ChirpsInterfaceTest>'
test/integration/chirps_interface_test.rb:29:in `block in <class:ChirpsInterfaceTest>'
我知道路由在应用程序中有效,因为我已经通过本地主机多次通过回复和重新发送成功提交。我如何通过 rails 测试来测试这些操作是否正常工作?
Error: ChirpsInterfaceTest#test_chirp_interface:
ActionController::UnknownFormat: ChirpsController#reply is missing a
template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
您的链接有 remote: true
。这意味着请求是 JS
。要测试此类链接,您应该像这样使用 xhr: true
get reply_chirp_path(@user, parent_id: @chirp), xhr: true
由于您没有指定它,请求被视为 HTML
并因该异常而失败。
有关详细信息,请阅读 Testing XHR (AJAX) requests
我正在为名为 "Canary" 的 Twitter 克隆应用程序创建测试套件,但我很难为 chirps#reply 和 chirps#rechirps
编写集成测试Chirps 控制器
def reply
@chirp = current_user.chirps.new
@parent = Chirp.find_by(id: params[:parent_id])
@chirp.parent_id = params[:parent_id]
respond_to do |format|
format.js
format.html
end
end
def rechirp
@chirp = current_user.chirps.new
@reference = Chirp.find_by(id: params[:reference_id])
@chirp.reference_id = params[:reference_id]
respond_to do |format|
format.js
format.html
end
end
啁啾视图
<%= link_to reply_chirp_path(current_user, parent_id: chirp.id), remote: true, data: { target: '#modal_container', toggle: 'modal' } do %>
<%= icon('far', 'comment') %> <%= chirp.children.size if chirp.children.size > 0 %>
<% end %>
<%= link_to rechirp_chirp_path(current_user, reference_id: chirp.id), remote: true, data: { target: '#modal_container', toggle: 'modal' } do %>
<%= icon('fas', 'retweet') %> <%= count_rechirps(chirp) %>
<% end %>
Reply.js.erb
$("#modal_container").find(".modal-content").html("<%= j render 'chirps/modal_reply' %>");
$("#modal_container").modal('show', 'focus');
Rechirp.js.erb
$("#modal_container").find(".modal-content").html("<%= j render 'chirps/modal_rechirp' %>");
$("#modal_container").modal('show', 'focus');
Chirps 接口测试
# Try to reply to chirp
assert_select 'a[href=?]', reply_chirp_path(@user, parent_id: @chirp.id)
assert_difference 'Chirp.count', 1 do
get reply_chirp_path(@user, parent_id: @chirp)
assert_select 'div#modal_container > div.modal-dialog > div.modal-content > div.modal-body'
post chirps_path, params: { chirp: { content: content, parent_id: @chirp.id } }
assert :success
end
assert_redirected_to root_url
follow_redirect!
# Try to rechirp chirp
assert_select 'a[href=?]', rechirp_chirp_path(@user, reference_id: @chirp.id)
当我 运行 chirp_interface_test.rb 时,我一直遇到同样的错误:
Error: ChirpsInterfaceTest#test_chirp_interface:
ActionController::UnknownFormat: ChirpsController#reply is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
test/integration/chirps_interface_test.rb:30:in `block (2 levels) in <class:ChirpsInterfaceTest>' test/integration/chirps_interface_test.rb:29:in `block in <class:ChirpsInterfaceTest>'
我知道路由在应用程序中有效,因为我已经通过本地主机多次通过回复和重新发送成功提交。我如何通过 rails 测试来测试这些操作是否正常工作?
Error: ChirpsInterfaceTest#test_chirp_interface:
ActionController::UnknownFormat: ChirpsController#reply is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
您的链接有 remote: true
。这意味着请求是 JS
。要测试此类链接,您应该像这样使用 xhr: true
get reply_chirp_path(@user, parent_id: @chirp), xhr: true
由于您没有指定它,请求被视为 HTML
并因该异常而失败。
有关详细信息,请阅读 Testing XHR (AJAX) requests