ruby-on-rails 如果Rails不抛出错误,我将无法运行任何测试

w41d8nur  于 2023-01-14  发布在  Ruby
关注(0)|答案(1)|浏览(159)

我目前正在研究测试,我尝试做一个简单的资产_not test,并注意到我运行$ rails test test/models/tweet_test.rb
我得到:

Error:
TweetTest#test_should_not_create_tweet_without_user:
ActiveRecord::NotNullViolation: RuntimeError: NOT NULL constraint failed: comments.tweet_id

推特_测试. rb

require "test_helper"

class TweetTest < ActiveSupport::TestCase
  test "should not create tweet without user" do
    tweet = Tweet.new
    assert_not tweet.save, "Tweet is able to save without user association"
  end
end

tweet.rb

belongs_to :user
  belongs_to :tweet, optional: true
  has_many :comments, dependent: :destroy
  has_many :likes, as: :likeable

  validates :body, length: { maximum: 240 }, allow_blank: false, unless: :tweet_id

comment.rb

belongs_to :user
  belongs_to :tweet
  has_many :likes, as: :likeable
  validates :body, length: { maximum: 240 }, allow_blank: false

图式

ActiveRecord::Schema[7.0].define(version: 2023_01_07_162453) do
  create_table "comments", force: :cascade do |t|
    t.integer "tweet_id", null: false
    t.bigint "user_id", null: false
    t.text "body", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["tweet_id"], name: "index_comments_on_tweet_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end

  create_table "likes", force: :cascade do |t|
    t.bigint "user_id", null: false
    t.integer "likeable_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "likeable_type"
    t.index ["likeable_id", "likeable_type"], name: "index_likes_on_likeable_id_and_likeable_type"
    t.index ["likeable_id"], name: "index_likes_on_likeable_id"
    t.index ["user_id", "likeable_id", "likeable_type"], name: "index_likes_on_user_id_and_likeable_id_and_likeable_type", unique: true
    t.index ["user_id"], name: "index_likes_on_user_id"
  end

  create_table "tweets", force: :cascade do |t|
    t.bigint "user_id"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "tweet_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email", default: "", null: false
    t.boolean "admin", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.string "username"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "comments", "tweets"
  add_foreign_key "comments", "users"
  add_foreign_key "likes", "users"
end

运行Rails时堆栈跟踪

rails  test test/models/tweet_test.rb
D, [2023-01-06T22:08:48.613375 #7565] DEBUG -- :    (0.0ms)  PRAGMA foreign_keys
D, [2023-01-06T22:08:48.613423 #7565] DEBUG -- :    (0.0ms)  PRAGMA defer_foreign_keys
D, [2023-01-06T22:08:48.613457 #7565] DEBUG -- :    (0.0ms)  PRAGMA defer_foreign_keys = ON
D, [2023-01-06T22:08:48.613481 #7565] DEBUG -- :    (0.0ms)  PRAGMA foreign_keys = OFF
D, [2023-01-06T22:08:48.613554 #7565] DEBUG -- :   TRANSACTION (0.0ms)  begin transaction
D, [2023-01-06T22:08:48.613984 #7565] DEBUG -- :   Fixtures Load (0.4ms)  DELETE FROM "comments";
DELETE FROM "tweets";
DELETE FROM "users";
INSERT INTO "comments" ("id", "created_at", "updated_at") VALUES (980190962, '2023-01-07 03:08:48.611501', '2023-01-07 03:08:48.611501');
INSERT INTO "comments" ("id", "created_at", "updated_at") VALUES (298486374, '2023-01-07 03:08:48.611501', '2023-01-07 03:08:48.611501');
INSERT INTO "tweets" ("id", "created_at", "updated_at") VALUES (980190962, '2023-01-07 03:08:48.612217', '2023-01-07 03:08:48.612217');
INSERT INTO "tweets" ("id", "created_at", "updated_at") VALUES (298486374, '2023-01-07 03:08:48.612217', '2023-01-07 03:08:48.612217');
INSERT INTO "users" ("id", "created_at", "updated_at") VALUES (980190962, '2023-01-07 03:08:48.612632', '2023-01-07 03:08:48.612632');
INSERT INTO "users" ("id", "created_at", "updated_at") VALUES (298486374, '2023-01-07 03:08:48.612632', '2023-01-07 03:08:48.612632')
D, [2023-01-06T22:08:48.614166 #7565] DEBUG -- :   TRANSACTION (0.1ms)  rollback transaction
D, [2023-01-06T22:08:48.614213 #7565] DEBUG -- :    (0.0ms)  PRAGMA defer_foreign_keys = 0
D, [2023-01-06T22:08:48.614237 #7565] DEBUG -- :    (0.0ms)  PRAGMA foreign_keys = 1

测试/应用系统测试用例. rb

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end

/测试/夹具/备注. yml

comment: 
 id: 1
 tweet_id: 1
 user_id: 1
 body: "test comment"

/测试/夹具/tweet. yml

tweet: 
 body: "test tweet"
 user_id: 1
 id: 1

测试/夹具/用户. yml

user: 
 id: 1
 name: "tester"
 email: "email@test.com"
 username: "testingman"
 encrypted_password: 123456

注解迁移文件

class CreateComments < ActiveRecord::Migration[7.0]
  def change
    create_table :comments do |t|
      t.references :tweet, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
      t.text :body, null: false
      t.timestamps
    end
  end
end

我在我的第一个项目中使用了一点集成测试,现在我试图熟悉所有的测试,这真的让我很困惑。
谢谢,

33qvvth1

33qvvth11#

我的问题的答案相对简单,我删除了text/fixtures/comment.ymltext/fixtures/tweet.ymltext/fixtures/user.yml文件中的默认代码。
在所有三个文件中,默认代码为

one: {}
# column: value
#
two: {}
# column: value

我删除了所有这些。我的代码立即工作。我不知道为什么,但它的工作。我相信可能是空哈希正是这是根本原因,但它超出了我。

相关问题