未定义的局部变量或方法:: ERROR

时间:2014-07-25 10:49:47

标签: ruby-on-rails ruby variables methods undefined

我正在跑步' rake test'并且我得到了这个错误

1)错误: LineItemsControllerTest#test_should_create_line_item: NameError:未定义的局部变量或#的方法产品   test / controllers / line_items_controller_test.rb:21:在块(2级)中    test / controllers / line_items_controller_test.rb:20:in block in

订单项控制器测试的代码是

require 'test_helper'

class LineItemsControllerTest < ActionController::TestCase
  setup do
    @line_item = line_items(:one)
   end

   test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:line_items)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create line_item" do
    assert_difference('LineItem.count') do
    post :create, product_id: products(:ruby).id
    end

     assert_redirected_to cart_path(assigns(:line_item).cart)
  end

  test "should show line_item" do
    get :show, id: @line_item
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @line_item
    assert_response :success
  end

  test "should update line_item" do
    patch :update, id: @line_item, line_item: { cart_id: @line_item.cart_id, product_id:      @line_item.product_id }
    assert_redirected_to line_item_path(assigns(:line_item))
  end

  test "should destroy line_item" do
    assert_difference('LineItem.count', -1) do
      delete :destroy, id: @line_item
    end

    assert_redirected_to line_items_path
    end
 end

我不知道控制器文件中的代码是否也与它有关。在此错误之前,它提出了关于第3行的内容并且没有局部变量或方法

def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)

respond_to do |format|
  if @line_item.save
    format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
    format.json { render action: 'show', status: :created, location: @line_item }
  else
    format.html { render action: 'new' }
    format.json { render json: @line_item.errors, status: :unprocessable_entity }
  end
  end
 end

0 个答案:

没有答案