簡體   English   中英

Rails測試“ ActionController :: UnknownFormat:缺少此請求格式和變體的模板”

[英]Rails Test “ActionController::UnknownFormat: is missing a template for this request format and variant”

我正在為一個名為“ Canary”的Twitter克隆應用程序創建一個測試套件,並且很難編寫針對chirps#reply和chirps#rechirps的集成測試

線性調頻控制器

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

rp視圖

<%= 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');

線性調頻接口測試

# 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時,我總是遇到相同的錯誤:

錯誤:ChirpsInterfaceTest#test_chirp_interface:

ActionController :: UnknownFormat:ChirpsController#reply缺少此請求格式和變體的模板。

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測試中測試這些動作是否正常工作?

錯誤:ChirpsInterfaceTest#test_chirp_interface:

ActionController :: UnknownFormat:ChirpsController#reply缺少此請求格式和變體的模板。

request.formats:[“ text / html”]

request.variant:[]

您的鏈接具有remote: true 這意味着請求是JS 要測試這樣的鏈接,您應該像這樣使用xhr: true

get reply_chirp_path(@user, parent_id: @chirp), xhr: true

由於您未指定此選項,因此該請求被視為HTML並因該異常而失敗。

有關更多信息,請閱讀測試XHR(AJAX)請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM