开发者

How do I get the root directory of a gem in the Rspec tests for my Rails project?

I've got a gem, we'll call it ToastMitten, which I'm including in one of my Rails apps. I'm writing some tests for ToastMitten in which I need to load a file, and I want to specify a path from the root of the gem.

I just tried using Rails.root.to_s, but that gives me something like /Users/me/projects/toastmitten/spec/dummy. I would have expected that path to end at toastmitte开发者_StackOverflow中文版n/.

What am I doing wrong?


Rails.root.parent.to_s

If it always gives back your dummy Rails app, just move up to the parent.


It looks like you are using a Rails engine (generated with enginex, hence the dummy app in your spec folder). If you need to require a file in your test using an absolute path, you can use the following:

file = File.expand_path(File.join(File.dirname(__FILE__), 'path', 'to', 'file.ext'))

root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))

Can you be more specific on where you are trying to require this file? Also, The root example may require more ..'s.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜