【发布时间】:2018-04-11 00:40:59
【问题描述】:
我正在尝试在 minitest 中运行基本测试,但收到以下错误:
错误: 类别测试#test_category_should_be_valid: ActiveModel::UnknownAttributeError:类别的未知属性“名称”。 test/models/category_test.rb:5:in `setup'
但模型存在,数据库中存在表和行。
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
def setup
@category = Category.new(name: "sports")
end
test "category should be valid" do
assert @category.valid?
end
end
这是迁移表:
class CreateCategories < ActiveRecord::Migration[5.1]
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end
【问题讨论】:
-
您的
db/schema.rb文件对categories有什么看法? -
@Phil, create_table "categories", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end
-
对不起格式,但属性存在于数据库中,所以我不知道为什么我得到未知属性错误
-
如果你运行
bin/rails console,你可能会得到development环境。试试你的.new()电话。另一个提示是查看Category.columns_hash.keys并查看您的name是否在其中。 -
当我运行 Category.columns_hash.keys 我得到 ["id", "name", "created_at", "updated_at"]。所以它在那里。我还可以使用 .new 和 name 属性创建一个新类别,它可以工作。我想也许是测试有问题
标签: ruby-on-rails ruby minitest