简体   繁体   中英

with Rspec destroy is missing a template for this request format and variant

I have a Ruby on Rails application with a chat. the user creates a Chat and ActionCable updates the page on the user side. If the user wants to delete the Chat, ActionCable deletes the Chat on the user side. everything works perfectly. but if I write the test for that the test fails. this is my controller.

  def destroy
    chat = Chat.find(params[:id])
    chat.comments.delete_all
    chat.likes.delete_all
    id = '#chat-to-delete-id'+chat.id.to_s
    chat.delete
    ActionCable.server.broadcast 'room_channel',
                                delete: id
  end

This is my spec

  it 'delete a chat' do 
    sign_in(user)
    post :create, params: {chat:{body: 'hello'}}
    get :destroy, params: {id: Chat.last.id}
    expect(response.status).to eq (200)
  end

and this is the error I get when I run the test.

ActionController::UnknownFormat: PublicationsController#destroy is missing a template for this request format and variant

request.formats: ["text/html"] request.variant: []

NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

I don't need to render or redirect something because I update the page with ActionCable. if I do redirect_to something, the test is ok but it reloads the page.

Any help or suggestion to pass that test

If you're using the resources :chat route helper, then the destroy route by default doesn't respond to GET requests. If you run rails routes you'll see something like this

    chats GET    /chats(.:format)          chats#index
          POST   /chats(.:format)          chats#create
 new_chat GET    /chats/new(.:format)      chats#new
edit_chat GET    /chats/:id/edit(.:format) chats#edit
     chat GET    /chats/:id(.:format)      chats#show
          PATCH  /chats/:id(.:format)      chats#update
          PUT    /chats/:id(.:format)      chats#update
          DELETE /chats/:id(.:format)      chats#destroy

So, what you're actually requesting with get :destroy, params: {id: Chat.last.id} is the show action

try delete :destroy, params: {id: Chat.last.id} instead

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