简体   繁体   中英

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

I'm creating a testing suite for a Twitter clone app called "Canary" and I'm having a hard time writing integration tests for chirps#reply and chirps#rechirps

Chirps Controller

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

Chirp View

<%= 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 Interface Test

# 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)

When I run chirp_interface_test.rb I keep encountering the same error:

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>' 

I know that the routes work in app because I've successfully submitted by replies and rechirps multiple times through the localhost. How can I test that these actions work properly with rails testing?

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: []

Your links have remote: true . That means the request is JS . To test such links, you should use xhr: true like so

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

Since you haven't specified this, the request is treated as HTML and fails with that exception.

For more info, read Testing XHR (AJAX) requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM