title: How to write rspec test for json response from jquery file upload
body: <p>I have a form that uses the jquery-file-upload plugin.  It posts to a controller and the return objects are both json types.</p>
  
  <p>Controller</p>
  
  <pre><code>def create
    @upload = Upload.new(params[:upload])
    if @upload.save
      render :json =&gt; [ @upload.to_jq_upload ].to_json
    else
      render :json =&gt; [ @upload.to_jq_upload.merge({ :error =&gt; "custom_failure" }) ].to_json
    end
  end
  </code></pre>
  
  <p>I have the following rspec test:</p>
  
  <pre><code>describe "post 'create' " do
    before(:each) do
      @upload = Factory(:upload)
      @attr_upload = {:photo =&gt; sample_file}
    end
  
    describe "success" do
      it "should  create a new upload object" do
        lambda do
          post :create, :upload =&gt; @attr_upload, :format =&gt; :json
        end.should change(Upload, :count).by(1)
      end
    end
  end
  </code></pre>
  
  <p>which works. I would like to test both for failure as in a form was posted without a file or the file was too large or of the wrong format.  </p>
  
  <p>Here is my test.</p>
  
  <pre><code>describe "failure" do
    before(:each) do
      @upload = Factory(:upload)
      @attr_upload = {:photo =&gt; sample_file("too_big.jpg")}
    end
  
    it "should not create a new upload object" do
      lambda do
        post :create, :upload =&gt; @attr_upload, :format =&gt; :json
      end.should_not change(Upload, :count).by(1)
    end
  end
  </code></pre>
  
  <p>This does not work. Here is the error:</p>
  
  <pre><code>ActionController::RoutingError:
  No route matches {:action=&gt;"show", :controller=&gt;"uploads", :id=&gt;#&lt;Upload id: nil, created_at: nil, updated_at: nil, photo_file_name: "too_big.jpg", photo_content_type: "image/jpeg", photo_file_size: 7483326, photo_updated_at: "2011-07-19 02:24:06"&gt;}
  </code></pre>
  
  <p>Here is my routes file:</p>
  
  <pre><code>Hbg::Application.routes.draw do      
    resources :uploads
  end
  </code></pre>
  
  <p>Why am I getting this error when I am submitting the form to the 'create' action? The response is supposed to be a json object.</p>