2017-03-17 34 views
0

我試圖修復一些我在我的評論控制器中編寫的測試。截至目前,我目前的測試中,我得到這個錯誤:Rspec使用嵌套參數創建帖子

Failure/Error: @outlet = Outlet.find(params[:comment][:outlet_id]) 

ActiveRecord::RecordNotFound: 
    Couldn't find Outlet with 'id'= 

下面是一些測試的例子

describe '#create' do 
    context 'with valid attributes' do 
     before :each do 
      @outlet = FactoryGirl.create(:outlet) 
      @user = FactoryGirl.create(:user) 
      @comment_params = FactoryGirl.attributes_for(:comment) 
     end 

     let(:create) { post :create, params: { outlet_id: @outlet.id, user_id: @user.id, comment: @comment_params } } 

     it "creates new comment" do 
      expect { create }.to change { Comment.count }.by(1) 
     end 

     it "increases the post comment count by 1" do 
      expect { create }.to change { @outlet.comments.count }.by(1) 
     end 

     it "increases user comment count by 1" do 
      expect { create }.to change { @user.comments.count }.by(1) 
     end 
    end 
end 

我敢肯定這是因爲我在創建語句的發生我的測試

let(:create) { post :create, params: { outlet_id: @outlet.id, user_id: @user.id, comment: @comment_params } } 

,這裏是我的意見控制器創建行動

def create 
    @outlet = Outlet.find(params[:comment][:outlet_id]) 
    @comment = @outlet.comments.build(comment_params) 
    @comment.user_id = current_user.id 


    if @comment.save 
     redirect_to(@outlet) 
    end 
end 

我很確定它不工作,因爲它正在查找的outlet_id是comments參數中的嵌套參數。我將如何解決我的rspec測試,讓它尋找一個嵌套參數?

+0

與測試exmaples更新 –

回答

2

只是通過您的PARAMS作爲參數傳遞給post調用,嵌套在必要時,例如:

post :create, user_id: @user.id, comment: { outlet_id: @outlet.id }