rspec 测试中没有路由匹配 [英] no route matches in rspec test

查看:36
本文介绍了rspec 测试中没有路由匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试:

 它应该响应一个 json 对象"做xhr :post, :create, :upload =>@attr_upload, :format =>:json结尾

这是我的路线:

resources :uploads, :except =>[:new, :show]

我有一个用户可以使用 jquery-file-upload 插件上传图片的表单:

<%= form_for @upload, :html =>{ :multipart =>真 } 做 |f|%><div class="fileupload-buttonbar"><label class="fileinput-button"><span>添加文件...或删除它们以上传</span><%= f.file_field :photo, :id =>上传照片"%>

<%结束%>

在我的控制器中,我加载文件并生成一个 json 响应:

def 创建@upload = Upload.new(params[:upload])response_to do |格式|如果@upload.saveset_current_upload(@upload)format.json {render :json =>[@upload.to_jq_upload].to_json}别的format.json {render :json =>[ @upload.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json}结尾结尾结尾

当我运行测试时,我得到以下信息:

没有路由匹配 {:action=>"update", :controller=>"uploads", :id=>#}

谁能帮我完成这个测试?我正在提交到创建操作,为什么它尝试转到更新操作?

任何帮助将不胜感激我已经为此工作了两天.

解决方案

请求继续更新动作,而不是创建,所以用生成的表单仔细检查你的模板,他们的网址

I have the following test:

 it "should respond with a json object" do
            xhr :post, :create, :upload => @attr_upload, :format => :json
          end  

Here is my routes:

resources :uploads, :except =>[:new, :show]

I have a form that a user can upload images using the jquery-file-upload plugin:

<%= form_for @upload, :html => { :multipart => true } do |f| %>
<div class="fileupload-buttonbar">
    <label class="fileinput-button">
        <span>Add files... or drop them to upload</span>
            <%= f.file_field :photo, :id => "upload_photo" %>                
    </label>
</div>
<% end %>

In my controller I load the file and generate a json response:

def create
    @upload = Upload.new(params[:upload])
    respond_to do |format|
      if @upload.save
        set_current_upload(@upload)
        format.json {render :json => [ @upload.to_jq_upload ].to_json}
      else
        format.json {render :json => [ @upload.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json}
      end
    end
  end

When I run my test I am getting the following:

No route matches {:action=>"update", :controller=>"uploads", :id=>#<Upload id: nil, created_at: nil, updated_at: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, uploadable_id: nil, uploadable_type: nil>}

Can anyone help me get this test working? I am submitting to the create action so why is it trying to go to the update action?

Any help would be appreciated I have been working on this for two days.

解决方案

The request goes on update action, not create, so double check your templates with generated forms, their URLs

这篇关于rspec 测试中没有路由匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆