2016-04-22 34 views
0

我想使用Rspec在我的創建操作發送帖子,我有簡單的控制器邏輯。這是一個api應用程序,我運行我的rspec並返回422狀態。我試圖弄清楚我的規範是否將信息發送給控制器。這些是有問題的控制器/規格,我如何將信息插入到創建方法中。到目前爲止,所有的教程似乎都使用與我的相同的語法。在此先感謝使用Rspec發送創建方法的帖子

忽略硬編碼USER_ID在我的工廠,還在學習工廠和固定依賴那些

#post_controller  
def create 
    if User.exists?(params[:user_id]) 
    @post = Post.new(post_params) 
    @post.save 
    render json: @post, status: 200 
    else 
    render status: 422 
    end 
end 

#spec 
it "saves a new post in the database" do 
    attrs = attributes_for(:post) 
    post :create, post: attrs 
    expect(response.status).to eq(200) 
end 

#factory 
FactoryGirl.define do 
factory :post do 
    title "This is a new title" 
    body "This is the body" 
    user_id 1 
end 

回答

0

也許是你在params[:post][:user_id]user_id

+0

它包含在我的帖子params – Byrd

+0

是的,但你沒有達到post_params,如果User.exists?(params [:user_id]) – Ursus

+0

終點也需要user_id,這個檢查是錯誤的,當測試端點手動它工作正常。 – Byrd