I have this error in two of my tests:
test "should create question" do
assert_difference('Question.count') do
post :create, question: { question: 'apple', answer: "good", holder_id: 2}
end
end
test "should not create question" do
invalid_answer = "a" * 145
assert_difference('Question.count',0) do
post :create, question: { answer: invalid_answer }
end
assert_template 'new'
end
My create action
#create action
def create
@question = Question.new(params[:question])
@holder = Holder.find_by_id(@question.holder.id)
if @question.save
flash[:success] = "Question Saved"
redirect_to holder_path(@question.holder_id)
else
render 'new'
end
end
The stack trace shows it is on the create line both times. But, how come I get the Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id error?
Do I need to create an object first and then pass that in to the post create?
Yes you were right, you need to create the Holder instance before running this test.
But why do you create all the ivars, do you need them in new?
If not it seems the code can be dryed up to
HTH
Robert