db:fixtures:loadをspec/fixtures以下のファイルを見るようにする

RSpecを適用したとき、fixturesファイルもspec/fixturesディレクトリに置いてました。

だって、RSpecが生成するspec_helper.rbにもそのディレクトリに置くように書いてくれるじゃないですか。

config.fixture_path = "#{::Rails.root}/spec/fixtures"

・・・が、これだとrake db:fixtures:loadは失敗します。

原因は単純で、rake db:fixtures:loadはtest/fixtures以下しか見ないからです。

rake db:fixtures:loadがみるパスを変えたい!と思い、色々検索してみたのですが見つからない。

なんかいい手はないものかとなんとなく、rake -hとすると、興味深いものが。

tsubame.local{miyohide}% rake -h
rake [-f rakefile] {options} targets...

Options are ...
    -C, --classic-namespace          Put Task and FileTask in the top level namespace
    -D, --describe [PATTERN]         Describe the tasks (matching optional PATTERN), then exit.
    -n, --dry-run                    Do a dry run without executing actions.
(以下略)

お、なんか「-D」ってものがあるな。これ見ると詳細な説明が見れたりしないかな。

tsubame.local{miyohide}% rake -D db:fixtures:load 
rake db:fixtures:load
    Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures.

おぉ!ビンゴ。これみると、FIXTURES_PATHを指定してあげれば良いっぽい。ってか、まんまその通りが書いてある。

というわけで、fixturesファイルをspec/fixtures以下に置いたときにdb:fixtures:loadをうまく動かすためには

% rake db:fixtures:load FIXTURES_PATH=spec/fixtures

とすればOK。