レコードを削除するときにFOREIGN KEY constraint
エラーを処理するテストを作成したいが、ConstraintExceptionは発生しない。ConstraintExceptionがrspecで生成されない
class CreateCountries < ActiveRecord::Migration[5.1]
def change
create_table :countries do |t|
t.string :name
t.integer :sequence
end
end
end
class CreateStates < ActiveRecord::Migration[5.1]
def change
create_table :states do |t|
t.string :name
t.integer :sequence
t.references :country, foreign_key: true
end
end
end
以下のようなテーブルのスキーマに私はrails console
でそれを削除するにConstraintExceptionを高めることを確認しました。
c = Country.create!(name: 'country')
State.create!(name: 'state', country: c)
c.destroy
(0.1ms) "国" "国"。DELETE FROM取引 SQL(0.8ms)を開始します。 "ID" を=? [; "id"、10] (6.1ms)ロールバックトランザクション ActiveRecord :: InvalidForeignKey:SQLite3 :: ConstraintException:FOREIGN KEY制約が失敗しました: "countries"から削除 "countries"。 "id" =? from /Users/masamoto/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step '
しかし、rspecにエラーは発生せず、正常に削除されたように見えました。エラーが0.0339秒で完成
を破壊
describe 'Error' do
it 'Destroy' do
country = Country.create!(name: 'hohooho')
State.create!(name: 'oooooo', country: country)
country.destroy
end
end
は、私はトランザクションがコミットされていなかったと考え 1例、0失敗
(ファイルがロードする3.27秒かかった)、I設定を試したところ、config.use_transactional_fixtures = false
が変更されませんでした。
私は明示的なコミットを試みましたが、結果は同じです。
describe 'Error' do
it 'Destroy' do
country = nil
ActiveRecord::Base.transaction do
country = Country.create!(name: 'hohooho')
State.create!(name: 'oooooo', country: country)
end
ActiveRecord::Base.transaction do
country.destroy
end
end
end
アイデアはありますか?