Ruby on rails Rails模型测试:错误:[..]列中的null值违反了NOTNULL约束
我正在构建一个Rails 4(4.2.6)应用程序,现在我想为一个“place”模型指定一些小型测试,该模型表示打印在传单地图上的兴趣点。作为DB,我们通过“pg”gem使用Postgres(无论如何,这与测试无关)。不幸的是,在执行Ruby on rails Rails模型测试:错误:[..]列中的null值违反了NOTNULL约束,ruby-on-rails,postgresql,ruby-on-rails-4,Ruby On Rails,Postgresql,Ruby On Rails 4,我正在构建一个Rails 4(4.2.6)应用程序,现在我想为一个“place”模型指定一些小型测试,该模型表示打印在传单地图上的兴趣点。作为DB,我们通过“pg”gem使用Postgres(无论如何,这与测试无关)。不幸的是,在执行bundle exec-rake测试时,即使是像下面这样的简单测试也会导致错误:models class PlaceTest < ActiveSupport::TestCase def setup @place = Place.new(latitude
bundle exec-rake测试时,即使是像下面这样的简单测试也会导致错误:models
class PlaceTest < ActiveSupport::TestCase
def setup
@place = Place.new(latitude: 12, longitude: 52, name: 'foo', categories: 'bar')
end
test 'valid place is valid' do
assert @place.valid?
end
我不知道如何解释这个错误,因为latitude
根本不是空的。显然,这不可能是与数据库相关的错误,因为实例没有写入数据库。尽管如此,错误消息还是有这种声音。schema.rb文件如下所示:
ActiveRecord::Schema.define(version: 20160403170121) do
enable_extension "plpgsql"
create_table "descriptions", force: :cascade do |t|
t.integer "place_id"
t.string "language"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "descriptions", ["place_id"], name: "index_descriptions_on_place_id", using: :btree
create_table "places", force: :cascade do |t|
t.float "latitude", null: false
t.float "longitude", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name", null: false
t.string "categories", null: false
end
add_foreign_key "descriptions", "places"
end
顺便说一下:我可以在rails控制台中完美地实例化一个place对象,并检查.valid?
。如果有人能帮我解释这个错误信息,我会很高兴的,提前谢谢
编辑:我没有在我的模型文件中实现任何验证(实际上,我想使用测试驱动,只是测试工作不正常…发现错误,相应的YAML文件试图生成空的
放置
对象。问题已解决。发现错误,相应的YAML文件试图生成空的place
对象。问题解决了
ActiveRecord::Schema.define(version: 20160403170121) do
enable_extension "plpgsql"
create_table "descriptions", force: :cascade do |t|
t.integer "place_id"
t.string "language"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "descriptions", ["place_id"], name: "index_descriptions_on_place_id", using: :btree
create_table "places", force: :cascade do |t|
t.float "latitude", null: false
t.float "longitude", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name", null: false
t.string "categories", null: false
end
add_foreign_key "descriptions", "places"
end