DBで外部キー制約を設定している場合のTest::Unit::TestCase.fixtures

testやろうとしたら。。。色々大変ですね。
まぁそこは慣れてないだけだと自分に言い聞かせる。

require File.dirname(__FILE__) + '/../test_helper'

class AnkenTest < Test::Unit::TestCase
  fixtures :ankens

  def test_truth
    assert true
  end
end

$ ruby test/unit/anken_test.rb
Loaded suite test/unit/anken_test
Started
EWARNING:  there is no transaction in progress

Finished in 0.133593 seconds.

  1) Error:
test_truth(AnkenTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  insert or update on table "ankens" violates foreign key constraint "ankens_sales_staff_id_fkey"
DETAIL:  Key (sales_staff_id)=(40) is not present in table "staffs".

てな感じです。


そうDBで外部キー制約付けちゃってるんですよ。
まじめにやりすぎたようです。

class AnkenTest < Test::Unit::TestCase
  fixtures :staffs, :ankens
:

といった感じでエラーを見つつチマチマ fixtures に追加していってみています。

思い切って外部キー制約外してしまった方が幸せになれるのかなぁ。

RailsがよしなにやってくれるからDBで外部キー制約をつける必要ってなくね?」という意見多数。ふむ。