0

ディレクトリに minitest_helper.rb および mongoid.yml ファイルがあります。以下のコードを minitest_helper; に入れました。

require 'mongoid'
Mongoid.load!("mongoid.yml", :test)

これらのファイルは同じディレクトリにありますが、Mongoid は yml ファイルをロードできず、以下のような「no such file」が表示されました。

/home/developer/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/config
/environment.rb:40:in `initialize': No such file or directory - mongoid.yml
(Errno::ENOENT)

また、Rails や Sinatra などのフレームワークも使用していません。何が問題なのですか?

4

1 に答える 1

1

ドキュメントでわかるように、#load!にはファイルへのフル パスが必要です。コードを次のように変更してみてください。

Mongoid.load!(File.join('path_to_the_yml','starting_at_root_of_the_project', 'mongoid.yml') , :test)

を構築する方法はFile.join、ディレクトリ構造によって異なります。次のような構造があるとします。

project_root
--lib
--spec
----fixtures
------test.xml  # the path for this file is project_root/spec/fixtures/test.xml

次に、次のFile.joinようになります。

File.join('spec','fixtures', 'test.xml')
于 2013-04-09T14:40:28.730 に答える