title: How to test json login in devise with rspec
body: <p>I am trying to build a rails login process with devise that will allow the user to signin/signout through a mobile application.</p>
  
  <p>I created a SessionsController like that : </p>
  
  <pre><code>class SessionsController &lt; Devise::SessionsController
  
    def create  
      resource = warden.authenticate!(:scope =&gt; resource_name, :recall =&gt; "#{controller_path}#new")
      set_flash_message(:notice, :signed_in) if is_navigational_format?
      sign_in(resource_name, resource)
  
      respond_to do |format|  
        format.html { super }  
        format.json {  
          render :status =&gt; 200, :json =&gt; { :error =&gt; "Success", :user =&gt;  resource}.to_json  
        }  
      end  
    end
  
    def destroy  
      super  
    end
  end
  </code></pre>
  
  <p>My routes : </p>
  
  <pre><code>devise_for :users, :controllers =&gt; {:sessions =&gt; "sessions"}
  resources :users
  </code></pre>
  
  <p>Then I have the following spec to test the process : </p>
  
  <pre><code>require 'spec_helper'
  
  describe SessionsController do
  
    describe "POST 'signin'" do
  
      before (:each) do
        @user = Factory(:user)
      end
  
      it "should login with json request" do
        @expected = @user.to_json
  
        post :create, :user =&gt; {:email =&gt; 'user@test.com', :password =&gt; 'please'}, :content_type =&gt; 'application/json', :format =&gt; :json
        response.body.should == @expected
      end
  
    end
  
  end
  </code></pre>
  
  <p>And I get the following error :</p>
  
  <pre><code>Failure/Error: post :create, :user =&gt; {:email =&gt; 'user@test.com', :password =&gt; 'please'}, :content_type =&gt; 'application/json', :format =&gt; :json
       AbstractController::ActionNotFound:
         Could not find devise mapping for path "/users/sign_in.json?content_type=application%2Fjson&amp;user%5Bemail%5D=user%40test.com&amp;user%5Bpassword%5D=please".
         Maybe you forgot to wrap your route inside the scope block?
  </code></pre>
  
  <p>[EDIT]
  It seems like the functionality is ok but only the test is broken ; because if I run this small script : </p>
  
  <pre><code>require 'rest_client'
  require "active_support/core_ext"
  
  RestClient.post "http://localhost:3000/users/sign_in", {:user =&gt; {:email =&gt; 'user@test.com', :password =&gt; 'please'}}.to_json, :content_type =&gt; :json, :accept =&gt; :json
  </code></pre>
  
  <p>I get the following result : </p>
  
  <pre><code>"{\"error\":\"Success\",\"user\":{\"email\":\"user@test.com\",\"name\":\"First User\"}}"
  </code></pre>
  
  <p>which is the expected one. </p>