简体   繁体   English

Rspec嵌套控制器测试未将参数传递到我的控制器中

[英]Rspec nested controller test not passing params into my controller

I'm having trouble passing my params into a nested route through rspec. 我无法通过rspec将参数传递到嵌套路由中。 I'm using Rails 5 and Rspec 3.5 我正在使用Rails 5和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? 有谁知道为什么我的产品参数没有被传递?

The first Hash is the params for the test: 第一个Hash是测试的params

Try it: 试试吧:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM