title: cannot pass JSON into an RSpec test function
body: <p>I am trying to write test code for a login function, but the test code gives me an error. My rails code is just an API and sends and receives JSON:</p>
  
  <pre><code>     NoMethodError:
           undefined method `[]' for nil:NilClass
  </code></pre>
  
  <p>Here is the <code>Rspec</code> code I am trying to write:</p>
  
  <pre><code>describe 'POST doctors/login' do
      it 'successfully logs a doctor in' do
        login_params = {
            "login"=&gt;{
                "email" =&gt; "knkjn@aol.com",
                "password" =&gt; "123456"
            }
        }.to_json
  
        request_headers ={
            'Accept' =&gt; 'application/json',
            'Content_type' =&gt; 'application/json'
        }
  
        post 'login', login_params, request_headers
        expect(response.status).to eq 200
      end
  end
  </code></pre>
  
  <p>Below is the method from my controller that I am trying to make a test code for: </p>
  
  <pre><code>  def login
        response = Response.new
        this_doctor = Doctor.find_by_username(login_params["username"])
        if this_doctor &amp;&amp; this_doctor.authenticate(login_params["password"])
            response.data = {:remember_token =&gt; this_doctor.sign_in}
            response.code = true
        else
            response.code = false
            response.error = "Invalid Login"
        end
        render :json =&gt; response.json
    end
  </code></pre>
  
  <p>Below is how I accept the JSON request from the params</p>
  
  <pre><code>def login_params
      return nil unless params["login"]
      params.require(:login).permit(:email, :password)
  
  end
  </code></pre>
  
  <p>here is the <code>JSON</code> request (I've tested the JSON request using DHC Chrome plugin for POST requests, and I have verified that the login function in my controller works.):</p>
  
  <pre><code>{
      "login": {
          "email": "walasif@gmail.com",
          "password": "123456"
      }
  }
  </code></pre>
  
  <p>Is there anything special I have to do to write test code for an API in JSON? The the test code doesn't seem to be sending the params to my controller correctly.</p>