madav
madav

Reputation: 3086

Params is being nested in params in an Rspec post request

I am currently testing a post request for a nested route with Rspec request.

It is set up like this:

describe 'POST /url_contents' do
  let!(:role) { create(:role, name: "admin") }
  let(:valid_attributes) {
    {
      jwt: "SAMPLE",
      role: role
    }
  }

  context 'when the url is valid' do
    before { post "/#{role.name}/1", params: valid_attributes }

    it 'returns a status code of 201' do
      expect(response).to have_http_status(201)
    end

  end
end

I am hitting my controller, but when I check params, I am getting a nested params:

:params => { :params => { :jwt => "SAMPLE", :role => #<Role:0x0055959d32a888>" }, "controller"=> "auth"... }

How can I get the params to point to :jwt for my controller?

Upvotes: 2

Views: 248

Answers (2)

Make sure you are calling the right method (post). I have seen several times where there are multiple post methods in a project. Once I had a case where a method from the sinatra gem was used. I have also seen where a person copying into rspec test got the line include Rack::Test::Methods. I use pry for such tests. In pry you can use show-method get. In a case of rails it should return something like Rails::Controller::Testing::Integration

Upvotes: 0

madav
madav

Reputation: 3086

I've found a solution but would be happy to hear other ideas.

context 'when the jwt is valid' do
  before { post "/#{role.name}/1", {
    jwt: "SAMPLE",
    role: role
  }
}

Now params is not nested.

Upvotes: 1

Related Questions