【问题标题】:user test not working用户测试不起作用
【发布时间】:2018-05-08 19:35:59
【问题描述】:

所以这是我第一次在 Rails 中运行测试,这是我遇到的错误。

跑步:

E

错误:PostsControllerTest#test_should_show_post: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test test/controllers/posts_controller_test.rb:26

E

错误:PostsControllerTest#test_should_update_post: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test test/controllers/posts_controller_test.rb:36

E

错误:PostsControllerTest#test_should_create_post: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails 测试 test/controllers/posts_controller_test.rb:18

E

错误:PostsControllerTest#test_should_destroy_post: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test/controllers/posts_controller_test.rb:41

E

错误:PostsControllerTest#test_should_get_new: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test test/controllers/posts_controller_test.rb:13

E

错误:PostsControllerTest#test_should_get_index: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test test/controllers/posts_controller_test.rb:8

E

错误:PostsControllerTest#test_should_get_edit: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails 测试 test/controllers/posts_controller_test.rb:31

E

错误:用户测试#test_user_should_be_valid: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test/models/user_test.rb:8

E

错误:

HomeControllerTest#test_should_get_index: ActiveRecord::StatementInvalid: 找不到表“用户”

bin/rails test/controllers/home_controller_test.rb:4

在 0.316289 秒、28.4550 次运行/秒、0.0000 次断言/秒内完成。 9 次运行,0 次断言,0 次失败,9 次错误,0 次跳过

说找不到用户表,是什么原因。这是我的代码。

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def setup
    @user=User.new(name:"minhaj")
  end

  test "user should be valid" do
    assert @category.valid?
  end
end

类 DeviseCreateUsers
 def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

ActiveRecord::Schema.define(版本:20180413094453)做

  create_table "posts", force: :cascade do |t|
    t.datetime "date"
    t.string "name"
    t.integer "user_id"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.float "latitude"
    t.float "longitude"
    t.string "address"
  end

  create_table "rsvps", force: :cascade do |t|
    t.integer "user_id"
    t.integer "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["post_id"], name: "index_rsvps_on_post_id"
    t.index ["user_id"], name: "index_rsvps_on_user_id"
  end

# Could not dump table "users" because of following StandardError
#   Unknown type 'false' for column 'admin_role'

end

【问题讨论】:

  • assert @category.valid?@category 来自哪里?为什么不是@user
  • 你的权利我改变了它,但我仍然得到同样的错误。

标签: ruby-on-rails ruby unit-testing


【解决方案1】:

恭喜您通过测试。它总是比调试好。试试rake db:test:prepare

【讨论】:

  • 我已经尝试过了,但我仍然遇到同样的错误
  • rails/bin console 中你可以运行User.new(name: 'foo') 并且它可以工作,对吧?
  • 不,但当我将其更改为 User.new(email:"foo@hotmail.com") 时,它可以在 Rails 控制台上运行。所以我在我的测试中进行了更改,所以它显示的是电子邮件而不是名称,但我仍然遇到同样的错误
  • 有人建议bin/rails db:migrate,但将其删除。尝试一下,即使您已迁移。然后尝试(在bashRAILS_ENV=test bin/rails db:drop db:create
  • 我按照您所说的进行操作,但仍然遇到与之前所述相同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 2015-09-11
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多