ruby-on-rails - Rake 测试错误 - ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

标签 ruby-on-rails ruby testing activerecord rake-test

每次我做 rake 测试时,我都会得到 12 个错误,所有错误都具有相同的错误

8) 错误: 产品测试#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

最佳答案

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"

关于ruby-on-rails - Rake 测试错误 - ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934994/

相关文章:

ruby-on-rails - 你怎么修?格式 :label_method is no longer available

Ruby 数组按多个属性排序

ruby - 为什么 Class#allocate 在 Ruby 中是公共(public)方法?

mysql - Rails JSON API : limit has_many relationship to current_user

javascript - 如何在 form_remote_tag 的 :complete callback? 中获取表单标签的 JS DOM/JQuery 对象

ruby-on-rails - Nginx、 unicorn 和 Heroku

ruby-on-rails - 如何将长日期转换为日期?

testing - cargo 测试不显示任何输出也不显示所有测试

testing - JMeter 线程组中停止测试和立即停止测试单选框之间的区别

.net - 如何减少 .NET 中的 MSTest 执行时间?