我有一个项目在生产和开发方面运行良好,主要是在许多测试中运行良好,但是对于一个模型,无法运行测试。
型号:
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
(请注意,“cp”属性是一个数组,我在项目中使用的是 Postgres) (我没有在模型上设置“序列化:cp,数组”)
但是当我运行 rails test/models/city_test.rb 时,我得到的是
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";
我已经在这里搜索了其他解决方案,但没有解决问题。我猜测它与测试环境所做的 YAML 序列化有关,但我不太确定。
我对开发和生产环境完全没有问题,它只发生在测试环境上。
最佳答案
对于遇到相同情况的人。问题在于测试装置的声明。 YAML 中的数组必须以这种方式声明:
one:
city: Premià de Mar
cp:
- 08400
- 08401
two:
city: Mataró
cp:
- 08300
- 08301
其中“cp”是数组。正确设置阵列可解决此问题。
关于ruby-on-rails - Ruby on Rails 测试 - ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: 格式错误的数组文字:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57111038/