Ошибка теста Rake - ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

Каждый раз, когда я делаю тест рейка, я получаю 12 ошибок с одной и той же ошибкой.

8) Ошибка: ProductTest # test_image_url: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

9) Ошибка: ProductTest # test_product_attributes_must_not_be_empty: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

10) Ошибка: ProductTest # test_product_is_not_valid_without_a_unique_title: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

11) Ошибка: ProductTest # test_product_is_not_valid_without_a_unique_title _-_ i18n: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

12) Ошибка: ProductTest#test_product_price_must_be_positive: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

С первой строкой:

syntax error, unexpected ( arg, expecting keyword_do or '{' or '('
  post :create, product_id: products (:ruby).id

Вот мой файл yaml

one:
title: MyString
description: MyText
image_url: MyString
price: 9.99

two:
title: MyString
description: MyText
image_url: MyString
price: 9.99

ruby: 
title: "Programming Ruby 1.9"
description: "Ruby is the fastest growing and most exciting dynamic
language out there. If you need to get working programs
delivered fast, you should add Ruby to your toolbox."
price: 49.50
image_url: "ruby.png"

и соответствующие тестовые файлы

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

и это ошибки для этого файла

1) Ошибка: CartsControllerTest # test_should_create_cart: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

2) Ошибка: CartsControllerTest # test_should_destroy_cart: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

3) Ошибка: CartsControllerTest # test_should_get_edit: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

4) Ошибка: CartsControllerTest # test_should_get_index: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

5) Ошибка: CartsControllerTest # test_should_get_new: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

6) Ошибка: CartsControllerTest # test_should_show_cart: ActiveRecord:: Fixture:: FormatError: ActiveRecord:: Fixture:: FormatError

7) Ошибка: CartsControllerTest#test_should_update_cart: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

 require 'test_helper'

 class CartsControllerTest < ActionController::TestCase
  setup do
  @cart = carts(:one)
  end

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

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

   test "should create cart" do
   assert_difference('Cart.count') do
   post :create, cart: {  }
  end

  assert_redirected_to cart_path(assigns(:cart))
  end

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

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

  test "should update cart" do
  patch :update, id: @cart, cart: {  }
  assert_redirected_to cart_path(assigns(:cart))
  end

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

assert_redirected_to carts_path
  end
end

1 ответ

products это метод, который читает содержимое файла фикстуры. Запомните пространство между именем метода и аргументами. Попробуйте следующее:

post(:create, product_id: products(:ruby).id)

Синтаксис YAML разделен пробелом на основе отступа. Убедитесь, что атрибуты записи имеют отступы, например:

ruby:
  title: "Programming Ruby 1.9"
  description: "Ruby is the fastest growing and most exciting dynamic
  language out there. If you need to get working programs
  delivered fast, you should add Ruby to your toolbox."
  price: 49.50
  image_url: "ruby.png"
Другие вопросы по тегам