我的两个测试中有这个错误:
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
我的创建动作
#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
堆栈跟踪显示它两次都在创建行上。但是,我怎么会得到Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
错误?
我是否需要先创建一个对象,然后将其传递给 post create?