【问题标题】:trouble running basic test(unknown attribute error)运行基本测试时遇到问题(未知属性错误)
【发布时间】: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


【解决方案1】:

您的测试数据库可能与您当前的数据库架构不是最新的。要更新您的测试数据库,请运行

rails db:test:prepare

【讨论】:

    猜你喜欢
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    相关资源
    最近更新 更多