开发者

How to test lib files in Rails?

开发者 https://www.devze.com 2022-12-23 10:03 出处:网络
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 w

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

精彩评论

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

关注公众号