2017-04-21 166 views
0

我無法通過rspec將我的參數傳遞到嵌套路由。我使用的Rails 5和RSpec 3.5Rspec嵌套控制器測試不傳遞參數到我的控制器

我的規格如下所示:

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 

,並在我的控制我的PARAMS是這樣的:

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

有誰知道爲什麼我的產品PARAMS沒有被傳入?

回答

2

第一Hashparams用於測試:

試試:

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