0
我試着寫一個評論控制器的POST操作測試,但不斷收到此錯誤:Rspec的ActiveRecord的:: AssociationTypeMismatch
Failure/Error: @comment = @outlet.comments.build(comment_params)
ActiveRecord::AssociationTypeMismatch:
Outlet(#70273695598160) expected, got String(#70273689026640)
我試圖改變周圍的一些事情在試驗,但不斷收到同樣的錯誤。不知道什麼嘗試
這裏是我的測試:
describe 'create' do
context 'with valid attributes' do
before :each do
@outlet = FactoryGirl.create(:outlet)
@outlet_id = @outlet.id
@user = FactoryGirl.create(:user)
@user_id = @user.id
@comment_params = { body: "This is a comment", outlet: @outlet_id, user: @user_id }
end
let(:create) { post :create, params: { outlet_id: @outlet_id, user_id: @user_id, comment: @comment_params } }
it "creates new comment" do
puts @outlet.class
puts @user.class
expect { create }.to change { Comment.count }.by 1
end
end
end
這裏是我的意見控制器:
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@outlet = Outlet.find(params[:outlet_id])
@user = User.find(params[:user_id])
@comment = @outlet.comments.build(comment_params)
if @comment.save!
redirect_to(@outlet)
end
end
private
def comment_params
params.require(:comment).permit(:body, :outlet, :user)
end
end
你可以把一個斷點失敗前行,並檢查'comment_params'是什麼樣子? –