rspecを実行時のエラー
1) User with 2 or more comments orders them in reverse chronologically
Failure/Error: user = User.create!(:name => "hogehoge", :email => "gehogeho", :password => "gehogeho", :forign => 1)
ActiveRecord::InvalidForeignKey:
Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`test`.`users`, CONSTRAINT `fk_rails_8f8a4b3fcb` FOREIGN KEY (`forign`) REFERENCES `forign` (`id`))
# ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Mysql2::Error:
# Cannot add or update a child row: a foreign key constraint fails (`test`.`users`, CONSTRAINT `fk_rails_8f8a4b3fcb` FOREIGN KEY (`forign`) REFERENCES `forign` (`id`))
# ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.02786 seconds (files took 0.90184 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:5 # User with 2 or more comments orders them in reverse chronologically
どうやらテスト環境のデータベースにforignのidがないらしい
レコードだけをコピーする
mysql> insert into test.forign select * from development.forign
-> ;
エラー文が別の外部キーにidがないと言われたので成功したのだろう
出典
感想
テストについて知らなかったのでこれから取り組む