我目前正在通過敏捷開發與Rails 5,並且遇到了我的測試問題。也就是說,在期望產品的地方存在關聯不匹配但獲得字符串。我已經嘗試將line_items_controller_test.rb中的line_items_controller_test.rb從@ line_item.product修復到@ line_item.product_id,但無濟於事,並且儘可能多地挖掘了我的技能。Rails 5失敗測試ActiveRecord :: AssociationTypeMismatch
這裏是我的測試文件line_items_controller_test.rb
test "should update line_item" do
patch line_item_url(@line_item), params: { line_item: { cart_id: @line_item.cart_id, product: @line_item.product } }
assert_redirected_to line_item_url(@line_item)
end
,並從終端
Error:
LineItemsControllerTest#test_should_update_line_item:
ActiveRecord::AssociationTypeMismatch: Product(#70143083725900) expected, got String(#70143078473840)
app/controllers/line_items_controller.rb:47:in `block in update'
app/controllers/line_items_controller.rb:46:in `update'
test/controllers/line_items_controller_test.rb:40:in `block in <class:LineItemsControllerTest>'
bin/rails test test/controllers/line_items_controller_test.rb:39
我的測試結果失敗,這裏是它提到47和46的控制器本身開頭的respond_to
def update
respond_to do |format|
if @line_item.update(line_item_params)
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: @line_item }
else
format.html { render :edit }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
讓我知道是否有任何更多的信息,你需要。您也可以查看我的回購https://github.com/jamesemcc/depot
你可以發佈'line_item_params'方法嗎? – Pavan
您是否可以更新應用程序中的訂單項? –