开发者

How to test lib files in Rails?

I understand the benefit of putting classes, modules, etc. in the lib folder in Rails, but I haven't been able to find a clean way of testing these files. For the most part, it seems like unit tests would be the logical approach.

I guess my question is: W开发者_开发问答hat is the "rails way" for testing lib files?


Your lib directory is not automatically loaded by rails.

You can use ActiveSupport::Dependencies to override const_missing. Basically rails will try to load your constants when it boots, if they are undefined or not in memory it will look at your load paths.

If you have a file like my_class.rb, rails expects it to be MyClass.

The beauty of this is if you have some stuff in your lib directory, you don't have to require it with a relative path you can just say require 'something', instead of require 'lib/something'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜