As part of the RSpec, i need to reference a file contained in a gem I am depending on (call it gem foo). I control gem foo, so I can make changes to it as needed.
作为RSpec的一部分,我需要引用一个我依赖的gem中包含的文件(称之为gem foo)。我控制gem foo,所以我可以根据需要对它进行更改。
Gem 'foo' contains a file that I need to reference with in the a rspec spec. Is there a reasonably stable RubyGem or Bundler API to figure out 'foo' base directory?
Gem'foo'包含一个我需要在rspec规范中引用的文件。是否有一个相当稳定的RubyGem或Bundler API来找出'foo'基目录?
Assuming 'foo' is already required in my Gemfile: in Gemfile:
假设我的Gemfile中已经需要'foo':在Gemfile中:
gem 'foo'
I want to do something like this (in something_spec.rb):
我想做这样的事情(在something_spec.rb中):
filename = File.expand_path('examples/xml/result.xml', Gem.gem_base_path('foo'))
What is gem_base_path API call?
什么是gem_base_path API调用?
2 个解决方案
#1
2
I would recommend creating a function in your gem to do this:
我建议在gem中创建一个函数来执行此操作:
module Foo
class Configuration
def self.result_xml_path
File.realpath("../examples/xml/result.xml")
end
end
end
You can then do the following in your spec:
然后,您可以在规范中执行以下操作:
filename = Foo::Configuration.result_xml_path
This is much safer since you are getting all the information from the gem. It also looks cleaner.
这样更安全,因为您从gem获取所有信息。它看起来也更清洁。
#2
0
This may do what you need without ant need to touch the 'foo'
gem:
如果没有蚂蚁需要触摸'foo'宝石,这可能会做你需要的:
matches = Gem::Specification.find_all_by_name 'foo'
spec = matches.first
filename = File.expand_path('examples/xml/result.xml', spec.full_gem_path)
I have used this code to make something similar to what you need (namely loading in my specs some factories defined in a gem used by my project)
我已经使用这个代码来制作类似于你需要的东西(即在我的规范中加载我的项目使用的gem中定义的一些工厂)
#1
2
I would recommend creating a function in your gem to do this:
我建议在gem中创建一个函数来执行此操作:
module Foo
class Configuration
def self.result_xml_path
File.realpath("../examples/xml/result.xml")
end
end
end
You can then do the following in your spec:
然后,您可以在规范中执行以下操作:
filename = Foo::Configuration.result_xml_path
This is much safer since you are getting all the information from the gem. It also looks cleaner.
这样更安全,因为您从gem获取所有信息。它看起来也更清洁。
#2
0
This may do what you need without ant need to touch the 'foo'
gem:
如果没有蚂蚁需要触摸'foo'宝石,这可能会做你需要的:
matches = Gem::Specification.find_all_by_name 'foo'
spec = matches.first
filename = File.expand_path('examples/xml/result.xml', spec.full_gem_path)
I have used this code to make something similar to what you need (namely loading in my specs some factories defined in a gem used by my project)
我已经使用这个代码来制作类似于你需要的东西(即在我的规范中加载我的项目使用的gem中定义的一些工厂)