简体   繁体   中英

Ruby on Rails Testing - ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: malformed array literal:

I have a project that is running ok on production and development and mostly on many tests, but with one model there's no way to run the tests.

Model:

create_table "cities", force: :cascade do |t|
  t.string "city"
  t.string "cp", array: true
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "name"
  t.string "primary_cp"
end

(Note that "cp" attribute is an Array and I'm using Postgres on the project) (I'm NOT setting "serialize: cp, Array" on the model)

But when I run rails test/models/city_test.rb what I get is

Error:
CityTest#test_the_truth:
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  
malformed array literal: "MyString"
LINE 28: ...d_at") VALUES (980190962, 'MyString', 'MyString', 
'MyString'...
                                                          ^
DETAIL:  Array value must start with "{" or dimension information.
: DELETE FROM "homes";
DELETE FROM "categories";
DELETE FROM "volunteers";
DELETE FROM "donations";

I've already searched for other solutions here but it doesn't fix the problem. What I'm guessing is that it is something related to the YAML serialization that the test environment does, but I'm not quite sure.

I have no problem with development and production environments at all, it only happens on test env.

For those who came up with same situation. the problem was with the declaration of the test fixtures. An array in YAML must be declared this way:

one:
  city: Premià de Mar
  cp:
    - 08400
    - 08401

two:
  city: Mataró
  cp:
    - 08300
    - 08301

Where "cp" is the array. Setting up the array properly fixes the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM