Search code examples
ruby-on-railsrspecruby-on-rails-5rspec-rails

Rspec nested controller test not passing params into my controller


I'm having trouble passing my params into a nested route through rspec. I'm using Rails 5 and Rspec 3.5

My spec looks like this:

require 'rails_helper'

describe "POST /api/v1/companies/:company_id/products.json", type: :controller do
  let!(:user) { create(:company_user, address: create(:address)) }
  let!(:company) { create(:company, company_user: user) }
  let!(:product) { create(:product) }
  let!(:params) { FactoryGirl.attributes_for(:product) }  

  before do
    @controller = Api::V1::ProductsController.new
  end

  context "company_user signed in" do    
    before do
      auth_headers = user.create_new_auth_token
      request.headers.merge!(auth_headers)      
      sign_in user
    end

    it 'creates a new product' do
      post :create, { company_id: company.id }, { params: {product: product_params} }

      expect(response.status).to eq(200)
      expect(Product.count).to eq(1)
    end 
  end

end

and in my controller my params look like this:

[1] pry(#<Api::V1::ProductsController>)> params
=> <ActionController::Parameters {"company_id"=>"1", "controller"=>"api/v1/products", "action"=>"create"} permitted: false>

Does anyone know why my product params are not being passed in?


Solution

  • The first Hash is the params for the test:

    Try it:

    post :create, { company_id: company.id, product: product_params }