「lib」にロードエラーを含む一連の頭痛を引き起こした新しいファイルを作成しました。「lib」ネームスペース/オートローディングで一貫性のない「LoadError」動作
/lib/response_set.rb:それは最初に到達したときに
module MyCompany
class ResponseSet < Array
...
end
end
/spec/lib/response_set_spec.rb
require 'spec_helper'
describe MyCompany::ResponseSet do
describe "..." do
...
end
end
はRSpecの中に、この仕様を実行すると、「私たちに次のエラーを与えます記述する:
/Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant': Expected /Users/my_stuff/projects/my_project/lib/response_set.rb to define ResponseSet (LoadError)
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-expectations-2.5.0/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
from /Users/my_stuff/projects/my_project/spec/lib/response_set_spec.rb:4:in `<top (required)>'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `block in load_spec_files'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'
どのように!私たちは長い間、同じ構造を持つ他の多くのファイルを使用してきました。
/lib/smart_set.rb
module MyCompany
class SmartSet < Array
...
end
end
とは
require 'spec_helper'
describe MyCompany::SmartSet do
describe "..." do
...
end
end
これを/spec/lib/smart_set_spec.rb:たとえば、ここではそれが作成されましたので、細かい作業をされてきた別の一つですファイルは同じ構造を持ちますが、まったく問題はありません。
ResponseSet(問題クラス)は、識別可能な理由がないため、明らかに読み込みの問題があります。レールコンソールで、私は1つを作成しようとする最初の時間は、私はエラーを取得し、その後私は後で1を作成することができます。
Loading development environment (Rails 3.0.4)
ruby-1.9.2-p136 :001 > rs = MyCompany::ResponseSet.new
LoadError: Expected /Users/my_stuff/projects/my_project/lib/response_set.rb to define ResponseSet
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
from (irb):1
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
from /Users/my_stuff/.rvm/gems/[email protected]_project/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
ruby-1.9.2-p136 :002 > rs = MyCompany::ResponseSet.new
=> []
をまた、response_set_specの上部に
require 'response_set'
を追加します。 rbはそれらのテストを実行できるようにします。しかし、smart_set_spec.rbにはそのようなことは必要ありません。
次はapplication.rb内の所定の位置にある:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.autoload_paths += Dir["#{config.root}/app/models/**/"]
は今、私はRailsは、ファイル構造は、物事のこれらのタイプの名前空間の構造と一致する必要があります方法についての意見のいくつかの並べ替えを持っていることを理解し、私たちは再構築しています私たちのモジュールとファイルはこの目的のために作られました。それは問題を解決したことを示しています(ただし、完全なテストスイートを実行している間に奇妙な負荷エラーが発生していましたが、これは不思議なことです)。それにもかかわらず、ここの誰もがうんざりしていて、Railsが矛盾していて少し悩まされているわけではありません。あなたが見ることができるように、名前空間とファイル構造に関して全く同じ2つのファイルがあり、完全に異なった扱いをしています。実際、私たちには、全く問題を引き起こさなかった類似の名前空間を持つ、「lib」の最上位レベルの約12のファイルがあります。誰でもここで何が起こっているのか説明できますか?
ありがとうございました。それは疑わしいと思われます。 Exceptionを呼び出すことはweather 'require_or_load'に依存しているはずです。しかし、再び私はRailsの勇気である迷路にはあまり慣れていません。 –
@Nick、明らかに、ファイルをロードした後に定数がまだ定義されていないときに例外が発生します。あなたの場合、ファイルは 'MyCompany :: ResponseSet'を定義している間に' ResponseSet'を定義すると思われます(これは私の見た目ですが、私は専門家ではありません)。不一致は最も奇妙なことです。元の問題を単純化していないことを確かめていますか?多分 'MyCompany :: SmartSet'は'/lib/smart_set.rb'だけでなく '/ lib/my_company/smart_set.rb'にありますか?私は[GitHubのバグ報告](https://github.com/rails/rails/issues/2572)に同様の問題について提出しました。 – Alexey
Nope。ファイル構造は同一でした。私たちは思っていたすべての可能な違いをチェックするのに数時間を費やしました。唯一の違いは、各クラス(つまり属性とメソッド)の実際の実装にありますが、ファイルの読み込み方法には影響しません! –