简体   繁体   English

与Rspec销毁缺少此请求格式和变体的模板

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

I have a Ruby on Rails application with a chat. 我有一个聊天的Ruby on Rails应用程序。 the user creates a Chat and ActionCable updates the page on the user side. 用户创建一个Chat,然后ActionCable在用户端更新页面。 If the user wants to delete the Chat, ActionCable deletes the Chat on the user side. 如果用户要删除聊天,则ActionCable会在用户端删除聊天。 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 ActionController :: UnknownFormat:PublicationsController#destroy缺少此请求格式和变体的模板

request.formats: ["text/html"] request.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. 对于XHR / Ajax或API请求,此操作通常会以204 No Content响应:白屏。 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. 如果您期望204 No Content,请继续。 That's what you'll get from an XHR or API request. 这就是您从XHR或API请求中获得的。 Give it a shot. 试一试。

I don't need to render or redirect something because I update the page with ActionCable. 我不需要渲染或重定向某些内容,因为我使用ActionCable更新了页面。 if I do redirect_to something, the test is ok but it reloads the page. 如果我将redirect_to设置为某项,则测试正常,但会重新加载页面。

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. 如果您使用resources :chat route helper,那么默认情况下, destroy路由不会响应GET请求。 If you run rails routes you'll see something like this 如果您沿rails routes ,将会看到类似的内容

    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 因此,您实际使用get :destroy, params: {id: Chat.last.id}请求是get :destroy, params: {id: Chat.last.id} show get :destroy, params: {id: Chat.last.id}

try delete :destroy, params: {id: Chat.last.id} instead 尝试delete :destroy, params: {id: Chat.last.id}代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 缺少此请求格式和变体的模板 - missing a template for this request format and variant 避免错误:缺少此请求格式和变体的模板 - avoiding the error: missing a template for this request format and variant Rails测试“ ActionController :: UnknownFormat:缺少此请求格式和变体的模板” - Rails Test “ActionController::UnknownFormat: is missing a template for this request format and variant” Rails ajax聊天应用程序缺少此请求格式和变体的模板 - Rails ajax chat app missing a template for this request format and variant ParksController#create 缺少此请求格式和变体的模板。 request.formats: [“text/html”] request.variant: [] - ParksController#create is missing a template for this request format and variant. request.formats: [“text/html”] request.variant: [] ReporteController#opcionales缺少此请求格式和变体的模板。 request.formats:[“application / pdf”] request.variant:[] - ReporteController#opcionales is missing a template for this request format and variant. request.formats: [“application/pdf”] request.variant: [] ModelsController#index缺少此请求格式和变体的模板。 request.formats:[“ application / xlsx”] request.variant:[] - ModelsController#index is missing a template for this request format and variant. request.formats: [“application/xlsx”] request.variant: [] 如何解决错误TeamsController#load_users缺少此请求格式和变体的模板 - How to fix the error TeamsController#load_users is missing a template for this request format and variant 为什么我无法解决错误“WelcomeController # index is missing a template for this request format and variant。 '? - Why can't I resolve the error ' WelcomeController # index is missing a template for this request format and variant. '? JSON 缺少此请求格式的模板和来自使用 Stripe 的 React 的变体 - JSON missing a template for this request format and variant coming from React using Stripe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM