2016-12-14 58 views
0

我目前正在通过敏捷开发与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

+0

你可以发布'line_item_params'方法吗? – Pavan

+0

您是否可以更新应用程序中的订单项? –

回答

1

好的。不仅在你的测试中,而且在应用程序中有问题。

  1. https://github.com/jamesemcc/depot/blob/master/app/controllers/line_items_controller.rb#L75您应当允许product_id,不product这里。您无法从浏览器传递Product对象。
  2. 你应该通过product_id从视图和测试

祝您好运!

+0

真棒,谢谢unkmas!我认为这是因为当我生成LineItem时,意外地写了'product:reference'而不是'product:references'。 – jamesemcc

相关问题